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

94 lines
2.8 KiB
JavaScript
Raw Normal View History

2014-10-02 16:05:44 -04:00
'use strict';
var LoginCtrl = function($scope, $location, updateHandler, account, auth, email, keychain, dialog) {
// check for app update
updateHandler.checkForUpdate();
// initialize the user account
initializeUser();
2014-11-19 09:46:20 -05:00
2014-10-02 16:05:44 -04:00
function initializeUser() {
// init the auth modules
auth.init();
2014-10-02 16:05:44 -04:00
// get OAuth token from chrome
auth.getEmailAddress(function(err, info) {
if (err) {
dialog.error(err);
return;
}
2014-10-02 16:05:44 -04:00
// check if account needs to be selected
if (!info.emailAddress) {
goTo('/add-account');
return;
}
// initiate the account by initializing the email dao and user storage
account.init({
2014-10-02 16:05:44 -04:00
emailAddress: info.emailAddress,
realname: info.realname
}, function(err, availableKeys) {
if (err) {
dialog.error(err);
2014-10-02 16:05:44 -04:00
return;
}
2014-10-02 16:05:44 -04:00
redirect(availableKeys);
});
});
}
function redirect(availableKeys) {
if (availableKeys && availableKeys.publicKey && availableKeys.privateKey) {
// public and private key available, try empty passphrase
email.unlock({
keypair: availableKeys,
passphrase: undefined
}, function(err) {
if (err) {
goTo('/login-existing');
return;
}
auth.storeCredentials(function(err) {
if (err) {
return dialog.error(err);
}
2014-10-02 16:05:44 -04:00
goTo('/desktop');
});
});
} else if (availableKeys && availableKeys.publicKey && !availableKeys.privateKey) {
2014-10-02 16:05:44 -04:00
// check if private key is synced
keychain.requestPrivateKeyDownload({
2014-10-02 16:05:44 -04:00
userId: availableKeys.publicKey.userId,
keyId: availableKeys.publicKey._id,
}, function(err, privateKeySynced) {
if (err) {
dialog.error(err);
return;
}
2014-10-02 16:05:44 -04:00
if (privateKeySynced) {
// private key is synced, proceed to download
goTo('/login-privatekey-download');
2014-01-27 12:50:13 -05:00
return;
}
2014-10-02 16:05:44 -04:00
// no private key, import key file
goTo('/login-new-device');
});
2014-10-02 16:05:44 -04:00
} else {
// no public key available, start onboarding process
goTo('/login-initial');
}
2014-10-02 16:05:44 -04:00
}
function goTo(location) {
$scope.$apply(function() {
$location.path(location);
});
}
};
2014-10-08 06:34:34 -04:00
module.exports = LoginCtrl;