2016-09-21 01:57:24 +02:00
|
|
|
|
angular
|
|
|
|
|
.module('bit.accounts')
|
|
|
|
|
|
2016-09-28 05:19:33 +02:00
|
|
|
|
.controller('accountsRegisterController', function ($scope, $state, cryptoService, toastr, $q, apiService, utilsService,
|
|
|
|
|
$analytics) {
|
2016-09-22 19:07:06 +02:00
|
|
|
|
$scope.model = {};
|
2016-09-27 00:41:20 +02:00
|
|
|
|
utilsService.initListSectionItemListeners($(document));
|
2016-09-21 01:57:24 +02:00
|
|
|
|
$('#email').focus();
|
|
|
|
|
|
|
|
|
|
$scope.submitPromise = null;
|
|
|
|
|
$scope.submit = function (model) {
|
2016-09-22 18:50:27 +02:00
|
|
|
|
if (!model.email) {
|
2016-09-22 19:03:28 +02:00
|
|
|
|
toastr.error('Email address is required.', 'Errors have occurred');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (model.email.indexOf('@') === -1) {
|
|
|
|
|
toastr.error('Invalid email address.', 'Errors have occurred');
|
2016-09-22 18:50:27 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!model.masterPassword) {
|
2016-09-22 19:03:28 +02:00
|
|
|
|
toastr.error('Master password is required.', 'Errors have occurred');
|
2016-09-22 18:50:27 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (model.masterPassword !== model.masterPasswordRetype) {
|
2016-09-22 19:03:28 +02:00
|
|
|
|
toastr.error('Master password confirmation does not match.', 'Errors have occurred');
|
2016-09-22 18:50:27 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-21 01:57:24 +02:00
|
|
|
|
var email = model.email.toLowerCase();
|
|
|
|
|
var key = cryptoService.makeKey(model.masterPassword, email);
|
|
|
|
|
$scope.submitPromise = registerPromise(key, model.masterPassword, email, model.hint);
|
|
|
|
|
$scope.submitPromise.then(function () {
|
2016-09-28 05:19:33 +02:00
|
|
|
|
$analytics.eventTrack('Registered');
|
2016-09-21 01:57:24 +02:00
|
|
|
|
toastr.success('Your new account has been created! You may now log in.');
|
|
|
|
|
$state.go('login', { email: email, animation: 'in-slide-left' });
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function registerPromise(key, masterPassword, email, hint) {
|
|
|
|
|
return $q(function (resolve, reject) {
|
|
|
|
|
cryptoService.hashPassword(masterPassword, key, function (hashedPassword) {
|
|
|
|
|
var request = new RegisterRequest(email, hashedPassword, hint);
|
|
|
|
|
apiService.postRegister(request,
|
|
|
|
|
function () {
|
|
|
|
|
resolve();
|
|
|
|
|
},
|
|
|
|
|
function (error) {
|
|
|
|
|
reject(error);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|