ami/src/ami.js/mszauth.js

36 lines
1 KiB
JavaScript

#include commitment.js
#include xhr.js
var AmiMisuzuAuth = function(refreshUrl) {
let userId = null;
let authMethod = 'Misuzu';
let authToken = null;
return {
hasInfo: () => userId !== null && authToken !== null,
getUserId: () => userId,
getAuthToken: () => authToken,
getLine: () => `${authMethod} ${authToken}`,
getInfo: () => {
return {
method: authMethod,
token: authToken,
};
},
refresh: function() {
return new Commitment((success, fail) => {
$xhr.get(refreshUrl, { authed: true, type: 'json' })
.success(({ body }) => {
if(body.ok) {
userId = body.usr.toString();
authToken = body.tkn;
}
success(body);
})
.fail(fail)
.run();
});
},
};
};