2017-12-03 05:12:55 -08:00
|
|
|
const ConfigManager = require('./configmanager.js')
|
|
|
|
const Mojang = require('./mojang.js')
|
|
|
|
|
2018-01-18 20:45:50 -08:00
|
|
|
exports.addAccount = async function(username, password){
|
|
|
|
const session = await Mojang.authenticate(username, password, ConfigManager.getClientToken)
|
|
|
|
const ret = ConfigManager.addAuthAccount(session.selectedProfile.id, session.accessToken, username, session.selectedProfile.name)
|
|
|
|
ConfigManager.save()
|
|
|
|
return ret
|
2017-12-03 05:12:55 -08:00
|
|
|
}
|
|
|
|
|
2018-01-18 20:45:50 -08:00
|
|
|
exports.validateSelected = async function(){
|
|
|
|
const current = ConfigManager.getSelectedAccount()
|
|
|
|
const isValid = await Mojang.validate(current.accessToken, ConfigManager.getClientToken())
|
|
|
|
console.log(isValid)
|
|
|
|
if(!isValid){
|
|
|
|
try {
|
|
|
|
const session = await Mojang.refresh(current.accessToken, ConfigManager.getClientToken())
|
|
|
|
console.log('ses', session)
|
|
|
|
ConfigManager.updateAuthAccount(current.uuid, session.accessToken)
|
2017-12-03 05:12:55 -08:00
|
|
|
ConfigManager.save()
|
2018-01-18 20:45:50 -08:00
|
|
|
} catch(err) {
|
|
|
|
if(err && err.message === 'ForbiddenOperationException'){
|
|
|
|
return false
|
|
|
|
}
|
2017-12-03 05:12:55 -08:00
|
|
|
}
|
2018-01-18 20:45:50 -08:00
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return true
|
|
|
|
}
|
2017-12-03 05:12:55 -08:00
|
|
|
}
|