2014-10-02 16:05:44 -04:00
|
|
|
'use strict';
|
2013-09-15 10:24:14 -04:00
|
|
|
|
2014-12-17 16:41:57 -05:00
|
|
|
var LoginCtrl = function($scope, $timeout, $location, updateHandler, account, auth, email, keychain, dialog, appConfig) {
|
2013-09-15 10:24:14 -04:00
|
|
|
|
2014-12-17 16:41:57 -05:00
|
|
|
//
|
|
|
|
// Scope functions
|
|
|
|
//
|
2013-10-09 10:40:36 -04:00
|
|
|
|
2014-12-17 16:41:57 -05:00
|
|
|
$scope.init = function() {
|
|
|
|
// initialize the user account
|
|
|
|
return auth.init().then(function() {
|
|
|
|
// get email address
|
|
|
|
return auth.getEmailAddress();
|
2014-12-01 08:55:03 -05:00
|
|
|
|
2014-12-17 16:41:57 -05:00
|
|
|
}).then(function(info) {
|
|
|
|
// check if account needs to be selected
|
|
|
|
if (!info.emailAddress) {
|
|
|
|
return $scope.goTo('/add-account');
|
|
|
|
}
|
2014-12-01 08:55:03 -05:00
|
|
|
|
2014-12-17 16:41:57 -05:00
|
|
|
// initiate the account by initializing the email dao and user storage
|
|
|
|
return account.init({
|
|
|
|
emailAddress: info.emailAddress,
|
|
|
|
realname: info.realname
|
|
|
|
}).then(function(availableKeys) {
|
|
|
|
return redirect(availableKeys);
|
|
|
|
});
|
2014-12-16 10:14:16 -05:00
|
|
|
|
2014-12-17 16:41:57 -05:00
|
|
|
}).catch(dialog.error);
|
|
|
|
};
|
2014-10-02 16:05:44 -04:00
|
|
|
|
|
|
|
function redirect(availableKeys) {
|
2014-11-06 08:28:14 -05:00
|
|
|
if (availableKeys && availableKeys.publicKey && availableKeys.privateKey) {
|
|
|
|
// public and private key available, try empty passphrase
|
2014-12-17 18:27:03 -05:00
|
|
|
var passphraseIncorrect;
|
2014-12-16 10:14:16 -05:00
|
|
|
return email.unlock({
|
2014-11-06 08:28:14 -05:00
|
|
|
keypair: availableKeys,
|
|
|
|
passphrase: undefined
|
2014-12-16 10:14:16 -05:00
|
|
|
}).catch(function() {
|
2014-12-17 18:27:03 -05:00
|
|
|
passphraseIncorrect = true;
|
2014-12-16 10:14:16 -05:00
|
|
|
// passphrase set... ask for passphrase
|
|
|
|
return $scope.goTo('/login-existing');
|
2014-12-17 16:41:57 -05:00
|
|
|
|
|
|
|
}).then(function() {
|
2014-12-17 18:27:03 -05:00
|
|
|
if (passphraseIncorrect) {
|
|
|
|
return;
|
|
|
|
}
|
2014-12-17 16:41:57 -05:00
|
|
|
// no passphrase set... go to main screen
|
2014-12-17 18:27:03 -05:00
|
|
|
return auth.storeCredentials().then(function() {
|
|
|
|
return $scope.goTo('/account');
|
|
|
|
});
|
2014-11-06 08:28:14 -05:00
|
|
|
});
|
2014-12-16 10:14:16 -05:00
|
|
|
|
2015-02-10 08:36:30 -05:00
|
|
|
} else if (availableKeys && availableKeys.publicKey && !availableKeys.privateKey) {
|
2015-02-27 14:04:44 -05:00
|
|
|
// proceed to private key download
|
|
|
|
return $scope.goTo('/login-privatekey-download');
|
2014-12-16 10:14:16 -05:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
} else {
|
2014-11-06 08:28:14 -05:00
|
|
|
// no public key available, start onboarding process
|
2014-12-16 10:14:16 -05:00
|
|
|
return $scope.goTo('/login-initial');
|
2013-10-09 10:40:36 -04:00
|
|
|
}
|
2014-10-02 16:05:44 -04:00
|
|
|
}
|
|
|
|
|
2014-11-26 13:51:15 -05:00
|
|
|
$scope.goTo = function(location) {
|
2014-11-26 11:57:14 -05:00
|
|
|
return $timeout(function() {
|
2014-10-02 16:05:44 -04:00
|
|
|
$location.path(location);
|
|
|
|
});
|
2014-11-26 13:51:15 -05:00
|
|
|
};
|
2014-12-17 16:41:57 -05:00
|
|
|
|
|
|
|
//
|
|
|
|
// Start the app
|
|
|
|
//
|
|
|
|
|
|
|
|
// check for app update
|
|
|
|
updateHandler.checkForUpdate();
|
|
|
|
|
|
|
|
// init the app
|
|
|
|
if (!appConfig.preventAutoStart) {
|
|
|
|
$scope.init();
|
|
|
|
}
|
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
};
|
2013-09-15 10:24:14 -04:00
|
|
|
|
2014-10-08 06:34:34 -04:00
|
|
|
module.exports = LoginCtrl;
|