2014-10-02 16:05:44 -04:00
|
|
|
'use strict';
|
2013-09-15 10:24:14 -04:00
|
|
|
|
2014-11-26 11:57:14 -05:00
|
|
|
var LoginCtrl = function($scope, $timeout, $location, updateHandler, account, auth, email, keychain, dialog) {
|
2013-09-15 10:24:14 -04:00
|
|
|
|
2014-11-19 14:54:59 -05:00
|
|
|
// 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() {
|
2014-11-19 14:54:59 -05:00
|
|
|
// init the auth modules
|
2014-12-01 08:55:03 -05:00
|
|
|
auth.init(function(err) {
|
2013-10-09 10:40:36 -04:00
|
|
|
if (err) {
|
2014-12-01 08:55:03 -05:00
|
|
|
return dialog.error(err);
|
2013-10-09 10:40:36 -04:00
|
|
|
}
|
|
|
|
|
2014-12-01 08:55:03 -05:00
|
|
|
// get OAuth token from chrome
|
|
|
|
auth.getEmailAddress(function(err, info) {
|
2014-10-02 16:05:44 -04:00
|
|
|
if (err) {
|
2014-11-19 14:54:59 -05:00
|
|
|
dialog.error(err);
|
2014-10-02 16:05:44 -04:00
|
|
|
return;
|
|
|
|
}
|
2013-10-09 10:40:36 -04:00
|
|
|
|
2014-12-01 08:55:03 -05:00
|
|
|
// check if account needs to be selected
|
|
|
|
if (!info.emailAddress) {
|
|
|
|
$scope.goTo('/add-account');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// initiate the account by initializing the email dao and user storage
|
|
|
|
account.init({
|
|
|
|
emailAddress: info.emailAddress,
|
|
|
|
realname: info.realname
|
|
|
|
}, function(err, availableKeys) {
|
|
|
|
if (err) {
|
|
|
|
dialog.error(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
redirect(availableKeys);
|
|
|
|
});
|
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-11-19 14:54:59 -05:00
|
|
|
email.unlock({
|
2014-11-06 08:28:14 -05:00
|
|
|
keypair: availableKeys,
|
|
|
|
passphrase: undefined
|
|
|
|
}, function(err) {
|
|
|
|
if (err) {
|
2014-11-26 13:51:15 -05:00
|
|
|
$scope.goTo('/login-existing');
|
2014-11-06 08:28:14 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-19 14:54:59 -05:00
|
|
|
auth.storeCredentials(function(err) {
|
2014-11-06 08:28:14 -05:00
|
|
|
if (err) {
|
2014-11-19 14:54:59 -05:00
|
|
|
return dialog.error(err);
|
2014-11-06 08:28:14 -05:00
|
|
|
}
|
2014-10-02 16:05:44 -04:00
|
|
|
|
2014-12-02 10:12:42 -05:00
|
|
|
$scope.goTo('/account');
|
2014-11-06 08:28:14 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
} else if (availableKeys && availableKeys.publicKey && !availableKeys.privateKey) {
|
2014-10-02 16:05:44 -04:00
|
|
|
// check if private key is synced
|
2014-11-19 14:54:59 -05:00
|
|
|
keychain.requestPrivateKeyDownload({
|
2014-10-02 16:05:44 -04:00
|
|
|
userId: availableKeys.publicKey.userId,
|
|
|
|
keyId: availableKeys.publicKey._id,
|
|
|
|
}, function(err, privateKeySynced) {
|
2013-09-15 10:24:14 -04:00
|
|
|
if (err) {
|
2014-11-19 14:54:59 -05:00
|
|
|
dialog.error(err);
|
2013-09-15 10:24:14 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
if (privateKeySynced) {
|
|
|
|
// private key is synced, proceed to download
|
2014-11-26 13:51:15 -05:00
|
|
|
$scope.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
|
2014-11-26 13:51:15 -05:00
|
|
|
$scope.goTo('/login-new-device');
|
2013-09-15 10:24:14 -04: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-11-26 13:51:15 -05:00
|
|
|
$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-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;
|