mail/src/js/controller/login/login.js

84 lines
2.4 KiB
JavaScript
Raw Normal View History

2014-10-02 16:05:44 -04:00
'use strict';
2014-12-17 16:41:57 -05:00
var LoginCtrl = function($scope, $timeout, $location, updateHandler, account, auth, email, keychain, dialog, appConfig) {
2014-12-17 16:41:57 -05:00
//
// Scope functions
//
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-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-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-17 16:41:57 -05:00
}).catch(dialog.error);
};
2014-10-02 16:05:44 -04:00
function redirect(availableKeys) {
if (availableKeys && availableKeys.publicKey && availableKeys.privateKey) {
// public and private key available, try empty passphrase
2014-12-17 18:27:03 -05:00
var passphraseIncorrect;
return email.unlock({
keypair: availableKeys,
passphrase: undefined
}).catch(function() {
2014-12-17 18:27:03 -05:00
passphraseIncorrect = true;
// 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');
});
});
} else if (availableKeys && availableKeys.publicKey && !availableKeys.privateKey) {
// proceed to private key download
return $scope.goTo('/login-privatekey-download');
2014-10-02 16:05:44 -04:00
} else {
// no public key available, start onboarding process
return $scope.goTo('/login-initial');
}
2014-10-02 16:05:44 -04:00
}
2014-11-26 13:51:15 -05:00
$scope.goTo = function(location) {
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
};
2014-10-08 06:34:34 -04:00
module.exports = LoginCtrl;