Merge pull request #22 from Coding-Kiwi/master

Updated error handling in authenticator
This commit is contained in:
Pierce 2019-08-27 14:20:44 -04:00 committed by GitHub
commit 69a754721f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -3,7 +3,7 @@ const uuid = require('uuid/v1');
const api_url = "https://authserver.mojang.com";
module.exports.getAuth = function (username, password) {
return new Promise(resolve => {
return new Promise((resolve, reject) => {
if (!password) {
const user = {
access_token: uuid(),
@ -32,9 +32,9 @@ module.exports.getAuth = function (username, password) {
};
request.post(requestObject, function (error, response, body) {
if (error) resolve(error);
if(!body.selectedProfile) {
throw new Error("Validation error: " + response.statusMessage);
if (error) return reject(error);
if (!body || !body.selectedProfile) {
return reject("Validation error: " + response.statusMessage);
}
const userProfile = {
@ -52,7 +52,7 @@ module.exports.getAuth = function (username, password) {
};
module.exports.validate = function (access_token, client_token) {
return new Promise(resolve => {
return new Promise((resolve, reject) => {
const requestObject = {
url: api_url + "/validate",
json: {
@ -62,15 +62,16 @@ module.exports.validate = function (access_token, client_token) {
};
request.post(requestObject, async function (error, response, body) {
if (error) resolve(error);
if (error) return reject(error);
if(!body) resolve(true); else resolve(false);
if (!body) resolve(true);
else reject(body);
});
});
};
module.exports.refreshAuth = function (accessToken, clientToken, selectedProfile) {
return new Promise(resolve => {
return new Promise((resolve, reject) => {
const requestObject = {
url: api_url + "/refresh",
json: {
@ -82,9 +83,9 @@ module.exports.refreshAuth = function (accessToken, clientToken, selectedProfile
};
request.post(requestObject, function (error, response, body) {
if (error) resolve(error);
if(!body.selectedProfile) {
throw new Error("Validation error: " + response.statusMessage);
if (error) return reject(error);
if (!body || !body.selectedProfile) {
return reject("Validation error: " + response.statusMessage);
}
const userProfile = {
@ -101,7 +102,7 @@ module.exports.refreshAuth = function (accessToken, clientToken, selectedProfile
};
module.exports.invalidate = function (accessToken, clientToken) {
return new Promise(resolve => {
return new Promise((resolve, reject) => {
const requestObject = {
url: api_url + "/invalidate",
json: {
@ -111,15 +112,16 @@ module.exports.invalidate = function(accessToken, clientToken) {
};
request.post(requestObject, function (error, response, body) {
if (error) resolve(error);
if (error) return reject(error);
if(!body) resolve(true); else resolve(false);
if (!body) resolve(true);
else reject(body);
});
});
};
module.exports.signOut = function (username, password) {
return new Promise(resolve => {
return new Promise((resolve, reject) => {
const requestObject = {
url: api_url + "/signout",
json: {
@ -129,9 +131,10 @@ module.exports.signOut = function(username, password) {
};
request.post(requestObject, function (error, response, body) {
if (error) resolve(error);
if (error) return reject(error);
if(!body) resolve(true); else resolve(false);
if (!body) resolve(true);
else reject(body);
});
});
};