Updated error handling in authenticator

This commit is contained in:
CodingKiwi 2019-08-27 20:07:33 +02:00
parent 61f8af7a20
commit 18ac2852e2

View file

@ -3,8 +3,8 @@ const uuid = require('uuid/v1');
const api_url = "https://authserver.mojang.com"; const api_url = "https://authserver.mojang.com";
module.exports.getAuth = function (username, password) { module.exports.getAuth = function (username, password) {
return new Promise(resolve => { return new Promise((resolve, reject) => {
if(!password) { if (!password) {
const user = { const user = {
access_token: uuid(), access_token: uuid(),
client_token: uuid(), client_token: uuid(),
@ -31,10 +31,10 @@ module.exports.getAuth = function (username, password) {
} }
}; };
request.post(requestObject, function(error, response, body) { request.post(requestObject, function (error, response, body) {
if (error) resolve(error); if (error) return reject(error);
if(!body.selectedProfile) { if (!body || !body.selectedProfile) {
throw new Error("Validation error: " + response.statusMessage); return reject("Validation error: " + response.statusMessage);
} }
const userProfile = { const userProfile = {
@ -52,7 +52,7 @@ module.exports.getAuth = function (username, password) {
}; };
module.exports.validate = function (access_token, client_token) { module.exports.validate = function (access_token, client_token) {
return new Promise(resolve => { return new Promise((resolve, reject) => {
const requestObject = { const requestObject = {
url: api_url + "/validate", url: api_url + "/validate",
json: { json: {
@ -61,16 +61,17 @@ module.exports.validate = function (access_token, client_token) {
} }
}; };
request.post(requestObject, async function(error, response, body) { 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) { module.exports.refreshAuth = function (accessToken, clientToken, selectedProfile) {
return new Promise(resolve => { return new Promise((resolve, reject) => {
const requestObject = { const requestObject = {
url: api_url + "/refresh", url: api_url + "/refresh",
json: { json: {
@ -81,10 +82,10 @@ module.exports.refreshAuth = function (accessToken, clientToken, selectedProfile
} }
}; };
request.post(requestObject, function(error, response, body) { request.post(requestObject, function (error, response, body) {
if (error) resolve(error); if (error) return reject(error);
if(!body.selectedProfile) { if (!body || !body.selectedProfile) {
throw new Error("Validation error: " + response.statusMessage); return reject("Validation error: " + response.statusMessage);
} }
const userProfile = { const userProfile = {
@ -100,8 +101,8 @@ module.exports.refreshAuth = function (accessToken, clientToken, selectedProfile
}); });
}; };
module.exports.invalidate = function(accessToken, clientToken) { module.exports.invalidate = function (accessToken, clientToken) {
return new Promise(resolve => { return new Promise((resolve, reject) => {
const requestObject = { const requestObject = {
url: api_url + "/invalidate", url: api_url + "/invalidate",
json: { json: {
@ -110,16 +111,17 @@ module.exports.invalidate = function(accessToken, clientToken) {
} }
}; };
request.post(requestObject, function(error, response, body) { 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) { module.exports.signOut = function (username, password) {
return new Promise(resolve => { return new Promise((resolve, reject) => {
const requestObject = { const requestObject = {
url: api_url + "/signout", url: api_url + "/signout",
json: { json: {
@ -128,10 +130,11 @@ module.exports.signOut = function(username, password) {
} }
}; };
request.post(requestObject, function(error, response, body) { 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);
}); });
}); });
}; };