1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-10-19 07:35:48 +02:00
bitwarden-browser/src/services/userService.js
2016-09-03 01:13:09 -04:00

88 lines
2.4 KiB
JavaScript

function UserService(tokenService, apiService) {
this.tokenService = tokenService;
this.apiService = apiService;
};
!function () {
var _userProfile = null;
UserService.prototype.getUserProfile = function (callback) {
if (!callback || typeof callback !== 'function') {
throw 'callback function required';
}
if (_userProfile) {
callback(_userProfile);
return;
}
this.setUserProfile(null, function () {
callback(_userProfile);
});
};
UserService.prototype.setUserProfile = function (profile, callback) {
if (!callback || typeof callback !== 'function') {
throw 'callback function required';
}
this.tokenService.getToken(function (token) {
if (!token) {
return;
}
var decodedToken = this.tokenService.decodeToken(token);
var twoFactor = decodedToken.authmethod === "TwoFactor";
_userProfile = {
id: decodedToken.nameid,
email: decodedToken.email,
twoFactor: twoFactor
};
if (!twoFactor && profile) {
loadProfile(profile, callback);
}
else if (!twoFactor && !profile) {
this.apiService.getProfile(function (response) {
loadProfile(response, callback);
});
}
});
function loadProfile(profile, callback) {
_userProfile.extended = {
name: profile.name,
twoFactorEnabled: profile.twoFactorEnabled,
culture: profile.culture
};
callback();
}
};
UserService.prototype.clearUserProfile = function () {
_userProfile = null;
};
UserService.prototype.isAuthenticated = function (callback) {
if (!callback || typeof callback !== 'function') {
throw 'callback function required';
}
this.getUserProfile(function (profile) {
callback(profile !== null && !profile.twoFactor);
});
};
UserService.prototype.isTwoFactorAuthenticated = function (callback) {
if (!callback || typeof callback !== 'function') {
throw 'callback function required';
}
this.getUserProfile(function (profile) {
callback(profile !== null && profile.twoFactor);
});
};
}();