added validate and refresh endpoints to logic

This commit is contained in:
Pierce 2018-11-30 19:33:08 -05:00
parent 8d49706303
commit 0e09106643
2 changed files with 79 additions and 4 deletions

View file

@ -5,10 +5,37 @@ A script that launches Minecraft using NodeJS.
#### Usage #### Usage
##### Basic Login
```javascript ```javascript
const launcher = require('./pathtomodule'); const launcher = require('./pathtomodule');
launcher.authenticator("email", "password").then(auth => { launcher.authenticator("email", "password").then(auth => {
// Save the auth to a file so it can be used later on!
launcher.core({
authorization: auth,
// All of the following is required
root: "directory", // C:/Users/user/AppData/Roaming/.mc
os: "windows", // windows, osx, linux
version: {
number: "1.13.2", // Minecraft version you want to launch
type: "MCC-Launcher" // Type. Can be anything
},
memory: {
max: "500"
}
});
});
```
##### Using Validate and Refresh
```javascript
let auth = require("pathToUserAuthJson.json");
const validateCheck = await launcher.authenticator.validate(auth.access_token); // required arguments.
if(!validateCheck) {
auth = await launcher.authenticator.refreshAuth(auth.access_token, auth.client_token, auth.selected_profile); // required arguments.
}
launcher.core({ launcher.core({
authorization: auth, authorization: auth,
// All of the following is required // All of the following is required

View file

@ -41,6 +41,7 @@ function getAuth(username, password) {
client_token: uuid(), client_token: uuid(),
uuid: body.selectedProfile.id, uuid: body.selectedProfile.id,
name: body.selectedProfile.name, name: body.selectedProfile.name,
selected_profile: body.selectedProfile,
user_properties: JSON.stringify((body.user || {}).properties || {}) user_properties: JSON.stringify((body.user || {}).properties || {})
}; };
@ -49,6 +50,53 @@ function getAuth(username, password) {
}); });
} }
module.exports = async function(username, password) { function validate(access_token) {
return await getAuth(username, password); return new Promise(resolve => {
const requestObject = {
url: api_url + "/validate",
json: {
"accessToken": access_token
}
}; };
request.post(requestObject, async function(error, response, body) {
if (error) resolve(error);
if(!body) resolve(true); else resolve(false);
});
});
}
function refreshAuth(accessToken, clientToken, selectedProfile) {
return new Promise(resolve => {
const requestObject = {
url: api_url + "/refresh",
json: {
"accessToken": accessToken,
"clientToken": clientToken,
"selectedProfile": selectedProfile,
"requestUser": true
}
};
request.post(requestObject, function(error, response, body) {
if (error) resolve(error);
console.log(body);
if(!body.selectedProfile) {
throw new Error("Validation error: " + response.statusMessage);
}
const userProfile = {
access_token: body.accessToken,
client_token: uuid(),
uuid: body.selectedProfile.id,
name: body.selectedProfile.name,
user_properties: JSON.stringify((body.user || {}).properties || {})
};
resolve(userProfile);
});
});
}
module.exports = {getAuth, validate, refreshAuth};