2015-12-09 04:35:05 +01:00
|
|
|
angular
|
|
|
|
.module('bit.accounts')
|
|
|
|
|
2017-03-23 21:57:47 +01:00
|
|
|
.controller('accountsRegisterController', function ($scope, $location, apiService, cryptoService, validationService,
|
|
|
|
$analytics, $state) {
|
2016-03-09 03:20:21 +01:00
|
|
|
var params = $location.search();
|
2017-03-29 03:16:44 +02:00
|
|
|
var stateParams = $state.params;
|
2016-03-09 03:20:21 +01:00
|
|
|
|
2017-03-23 21:57:47 +01:00
|
|
|
$scope.returnState = $state.params.returnState;
|
2015-12-09 04:35:05 +01:00
|
|
|
$scope.success = false;
|
2016-03-09 03:20:21 +01:00
|
|
|
$scope.model = {
|
2017-03-29 03:16:44 +02:00
|
|
|
email: params.email ? params.email : stateParams.email
|
2016-03-09 03:20:21 +01:00
|
|
|
};
|
2017-03-29 03:16:44 +02:00
|
|
|
$scope.readOnlyEmail = stateParams.email !== null;
|
2015-12-09 04:35:05 +01:00
|
|
|
|
|
|
|
$scope.registerPromise = null;
|
2016-03-09 03:20:21 +01:00
|
|
|
$scope.register = function (form) {
|
2016-10-04 05:21:23 +02:00
|
|
|
var error = false;
|
|
|
|
|
2016-10-11 04:02:17 +02:00
|
|
|
if ($scope.model.masterPassword.length < 8) {
|
|
|
|
validationService.addError(form, 'MasterPassword', 'Master password must be at least 8 characters long.', true);
|
2016-10-04 05:21:23 +02:00
|
|
|
error = true;
|
|
|
|
}
|
2016-08-12 02:43:05 +02:00
|
|
|
if ($scope.model.masterPassword !== $scope.model.confirmMasterPassword) {
|
2016-03-09 03:20:21 +01:00
|
|
|
validationService.addError(form, 'ConfirmMasterPassword', 'Master password confirmation does not match.', true);
|
2016-10-04 05:21:23 +02:00
|
|
|
error = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error) {
|
2016-03-09 03:20:21 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-11 04:40:44 +02:00
|
|
|
var email = $scope.model.email.toLowerCase();
|
|
|
|
var key = cryptoService.makeKey($scope.model.masterPassword, email);
|
2017-02-21 05:59:12 +01:00
|
|
|
|
2017-04-14 00:18:32 +02:00
|
|
|
$scope.registerPromise = cryptoService.makeKeyPair(key).then(function (result) {
|
2017-02-21 05:59:12 +01:00
|
|
|
var request = {
|
|
|
|
name: $scope.model.name,
|
|
|
|
email: email,
|
|
|
|
masterPasswordHash: cryptoService.hashPassword($scope.model.masterPassword, key),
|
|
|
|
masterPasswordHint: $scope.model.masterPasswordHint,
|
|
|
|
keys: {
|
2017-04-14 00:18:32 +02:00
|
|
|
publicKey: result.publicKey,
|
|
|
|
encryptedPrivateKey: result.privateKeyEnc
|
2017-02-21 05:59:12 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-04-19 04:58:14 +02:00
|
|
|
return apiService.accounts.register(request).$promise;
|
2017-04-14 00:18:32 +02:00
|
|
|
}, function (errors) {
|
|
|
|
validationService.addError(form, null, 'Problem generating keys.', true);
|
|
|
|
return false;
|
|
|
|
}).then(function (result) {
|
|
|
|
if (result === false) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$scope.success = true;
|
|
|
|
$analytics.eventTrack('Registered');
|
2017-02-21 05:59:12 +01:00
|
|
|
});
|
2015-12-09 04:35:05 +01:00
|
|
|
};
|
|
|
|
});
|