var AmiMisuzuAuth = function(refreshUrl) { var userId = undefined, authToken = undefined; return { getUserId: function() { return userId; }, getAuthToken: function() { return authToken; }, getInfo: function() { return { method: 'Misuzu', token: authToken, }; }, getHttpAuth: function() { return 'Misuzu ' + authToken; }, refresh: function(callback) { var xhr = new XMLHttpRequest; xhr.onload = function() { if(xhr.status === 200) { var data = JSON.parse(xhr.responseText); if(data.ok === true) { userId = data.usr.toString(); authToken = data.tkn; } callback(data); } else callback({ok: null}); }; xhr.onerror = function() { callback({ok: null}); }; xhr.open('GET', refreshUrl); xhr.withCredentials = true; xhr.send(); }, }; };