1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-10-31 08:20:37 +01:00

convert auth service profile methods to promises

This commit is contained in:
Kyle Spearrin 2017-03-25 10:43:19 -04:00
parent 2154607d11
commit 19203e976b
8 changed files with 162 additions and 145 deletions

View File

@ -6,11 +6,7 @@ angular
require: 'ngModel', require: 'ngModel',
restrict: 'A', restrict: 'A',
link: function (scope, elem, attr, ngModel) { link: function (scope, elem, attr, ngModel) {
var profile = authService.getUserProfile(); authService.getUserProfile().then(function (profile) {
if (!profile) {
return;
}
// For DOM -> model validation // For DOM -> model validation
ngModel.$parsers.unshift(function (value) { ngModel.$parsers.unshift(function (value) {
if (!value) { if (!value) {
@ -35,6 +31,7 @@ angular
ngModel.$setValidity('masterPassword', valid); ngModel.$setValidity('masterPassword', valid);
return value; return value;
}); });
});
} }
}; };
}); });

View File

@ -10,7 +10,9 @@ angular
$scope.currentYear = new Date().getFullYear(); $scope.currentYear = new Date().getFullYear();
$scope.$on('$viewContentLoaded', function () { $scope.$on('$viewContentLoaded', function () {
vm.userProfile = authService.getUserProfile(); authService.getUserProfile().then(function (profile) {
vm.userProfile = profile;
});
if ($.AdminLTE) { if ($.AdminLTE) {
if ($.AdminLTE.layout) { if ($.AdminLTE.layout) {

View File

@ -6,7 +6,7 @@ angular
$scope.params = $state.params; $scope.params = $state.params;
if ($state.includes('backend.org')) { if ($state.includes('backend.org')) {
var userProfile = authService.getUserProfile(); authService.getUserProfile().then(function (userProfile) {
if (!userProfile.organizations || !userProfile.organizations.length) { if (!userProfile.organizations || !userProfile.organizations.length) {
return; return;
} }
@ -17,5 +17,6 @@ angular
break; break;
} }
} }
});
} }
}); });

View File

@ -58,16 +58,21 @@ angular
_service.getUserProfile = function () { _service.getUserProfile = function () {
if (!_userProfile) { if (!_userProfile) {
_service.setUserProfile(); return _service.setUserProfile();
} }
return _userProfile; var deferred = $q.defer();
deferred.resolve(_userProfile);
return deferred.promise;
}; };
_service.setUserProfile = function () { _service.setUserProfile = function () {
var deferred = $q.defer();
var token = tokenService.getToken(); var token = tokenService.getToken();
if (!token) { if (!token) {
return; deferred.reject();
return deferred.promise;
} }
var decodedToken = jwtHelper.decodeToken(token); var decodedToken = jwtHelper.decodeToken(token);
@ -77,30 +82,7 @@ angular
email: decodedToken.email email: decodedToken.email
}; };
apiService.accounts.getProfile({}, loadProfile); apiService.accounts.getProfile({}, function (profile) {
};
_service.addProfileOrganization = function (org) {
var profile = _service.getUserProfile();
if (profile) {
if (!profile.Organizations) {
profile.Organizations = [];
}
var o = {
id: org.Id,
name: org.Name,
key: org.Key,
status: org.Status
};
profile.organizations.push(o);
_userProfile = profile;
cryptoService.addOrgKey(o);
}
};
function loadProfile(profile) {
_userProfile.extended = { _userProfile.extended = {
name: profile.Name, name: profile.Name,
twoFactorEnabled: profile.TwoFactorEnabled, twoFactorEnabled: profile.TwoFactorEnabled,
@ -120,9 +102,36 @@ angular
_userProfile.organizations = orgs; _userProfile.organizations = orgs;
cryptoService.setOrgKeys(orgs); cryptoService.setOrgKeys(orgs);
deferred.resolve(_userProfile);
} }
}, function () {
deferred.reject();
});
return deferred.promise;
};
_service.addProfileOrganization = function (org) {
return _service.getUserProfile().then(function (profile) {
if (profile) {
if (!profile.Organizations) {
profile.Organizations = [];
} }
var o = {
id: org.Id,
name: org.Name,
key: org.Key,
status: org.Status
};
profile.organizations.push(o);
_userProfile = profile;
cryptoService.addOrgKey(o);
}
});
};
_service.isAuthenticated = function () { _service.isAuthenticated = function () {
return tokenService.getToken() !== null; return tokenService.getToken() !== null;
}; };

View File

@ -24,9 +24,9 @@
$scope.processing = true; $scope.processing = true;
var profile = authService.getUserProfile(); authService.getUserProfile().then(function (profile) {
var newKey = cryptoService.makeKey(model.newMasterPassword, profile.email.toLowerCase()); return cryptoService.makeKey(model.newMasterPassword, profile.email.toLowerCase());
}).then(function (newKey) {
var reencryptedLogins = []; var reencryptedLogins = [];
var loginsPromise = apiService.logins.list({ dirty: false }, function (encryptedLogins) { var loginsPromise = apiService.logins.list({ dirty: false }, function (encryptedLogins) {
var unencryptedLogins = cipherService.decryptLogins(encryptedLogins.Data); var unencryptedLogins = cipherService.decryptLogins(encryptedLogins.Data);
@ -59,6 +59,8 @@
toastr.error('Something went wrong.', 'Oh No!'); toastr.error('Something went wrong.', 'Oh No!');
}).$promise; }).$promise;
}); });
});
}; };
$scope.close = function () { $scope.close = function () {

View File

@ -36,15 +36,17 @@
$scope.generalSave = function () { $scope.generalSave = function () {
$scope.generalPromise = apiService.accounts.putProfile({}, $scope.model.profile, function (profile) { $scope.generalPromise = apiService.accounts.putProfile({}, $scope.model.profile, function (profile) {
authService.setUserProfile(profile); authService.setUserProfile(profile).then(function (updatedProfile) {
toastr.success('Account has been updated.', 'Success!'); toastr.success('Account has been updated.', 'Success!');
});
}).$promise; }).$promise;
}; };
$scope.passwordHintSave = function () { $scope.passwordHintSave = function () {
$scope.passwordHintPromise = apiService.accounts.putProfile({}, $scope.model.profile, function (profile) { $scope.passwordHintPromise = apiService.accounts.putProfile({}, $scope.model.profile, function (profile) {
authService.setUserProfile(profile); authService.setUserProfile(profile).then(function (updatedProfile) {
toastr.success('Account has been updated.', 'Success!'); toastr.success('Account has been updated.', 'Success!');
});
}).$promise; }).$promise;
}; };

View File

@ -4,13 +4,16 @@
.controller('settingsTwoFactorController', function ($scope, apiService, $uibModalInstance, cryptoService, authService, $q, toastr, $analytics) { .controller('settingsTwoFactorController', function ($scope, apiService, $uibModalInstance, cryptoService, authService, $q, toastr, $analytics) {
$analytics.eventTrack('settingsTwoFactorController', { category: 'Modal' }); $analytics.eventTrack('settingsTwoFactorController', { category: 'Modal' });
var _issuer = 'bitwarden', var _issuer = 'bitwarden',
_profile = authService.getUserProfile(), _profile = null,
_masterPasswordHash; _masterPasswordHash;
authService.getUserProfile().then(function (profile) {
_profile = profile;
$scope.account = _profile.email; $scope.account = _profile.email;
$scope.enabled = function () { $scope.enabled = function () {
return _profile.extended && _profile.extended.twoFactorEnabled; return _profile.extended && _profile.extended.twoFactorEnabled;
}; };
});
$scope.auth = function (model) { $scope.auth = function (model) {
_masterPasswordHash = cryptoService.hashPassword(model.masterPassword); _masterPasswordHash = cryptoService.hashPassword(model.masterPassword);

View File

@ -12,7 +12,7 @@
$scope.login = cipherService.decryptLogin(login); $scope.login = cipherService.decryptLogin(login);
}); });
var profile = authService.getUserProfile(); authService.getUserProfile().then(function (profile) {
if (profile && profile.organizations) { if (profile && profile.organizations) {
var orgs = []; var orgs = [];
for (var i = 0; i < profile.organizations.length; i++) { for (var i = 0; i < profile.organizations.length; i++) {
@ -39,6 +39,7 @@
$scope.subvaults = subvaults; $scope.subvaults = subvaults;
}); });
} }
});
$scope.submitPromise = null; $scope.submitPromise = null;
$scope.submit = function (model) { $scope.submit = function (model) {