2014-10-02 16:05:44 -04:00
|
|
|
'use strict';
|
2013-09-15 10:24:14 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
var appController = require('../app-controller');
|
2013-09-15 10:24:14 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
var LoginCtrl = function($scope, $location) {
|
2013-11-12 14:00:53 -05:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
// start main application controller
|
|
|
|
appController.start({
|
|
|
|
onError: $scope.onError
|
|
|
|
}, function(err) {
|
|
|
|
if (err) {
|
|
|
|
$scope.onError(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for app update
|
|
|
|
appController.checkForUpdate();
|
|
|
|
|
|
|
|
initializeUser();
|
|
|
|
});
|
|
|
|
|
|
|
|
function initializeUser() {
|
|
|
|
// get OAuth token from chrome
|
|
|
|
appController._auth.getEmailAddress(function(err, info) {
|
2013-10-09 10:40:36 -04:00
|
|
|
if (err) {
|
2013-11-12 14:00:53 -05:00
|
|
|
$scope.onError(err);
|
2013-10-09 10:40:36 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
// check if account needs to be selected
|
|
|
|
if (!info.emailAddress) {
|
|
|
|
goTo('/add-account');
|
|
|
|
return;
|
|
|
|
}
|
2014-09-19 15:51:37 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
// initiate controller by creating email dao
|
|
|
|
appController.init({
|
|
|
|
emailAddress: info.emailAddress,
|
|
|
|
realname: info.realname
|
|
|
|
}, function(err, availableKeys) {
|
|
|
|
if (err) {
|
|
|
|
$scope.onError(err);
|
|
|
|
return;
|
|
|
|
}
|
2013-10-09 10:40:36 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
redirect(availableKeys);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function redirect(availableKeys) {
|
|
|
|
// redirect if needed
|
|
|
|
if (typeof availableKeys === 'undefined') {
|
|
|
|
// no public key available, start onboarding process
|
|
|
|
goTo('/login-initial');
|
|
|
|
|
|
|
|
} else if (availableKeys && !availableKeys.privateKey) {
|
|
|
|
// check if private key is synced
|
|
|
|
appController._keychain.requestPrivateKeyDownload({
|
|
|
|
userId: availableKeys.publicKey.userId,
|
|
|
|
keyId: availableKeys.publicKey._id,
|
|
|
|
}, function(err, privateKeySynced) {
|
2013-09-15 10:24:14 -04:00
|
|
|
if (err) {
|
2013-11-12 14:00:53 -05:00
|
|
|
$scope.onError(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
|
|
|
|
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');
|
2013-09-15 10:24:14 -04:00
|
|
|
});
|
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
} else {
|
|
|
|
// public and private key available, try empty passphrase
|
|
|
|
appController._emailDao.unlock({
|
|
|
|
keypair: availableKeys,
|
|
|
|
passphrase: undefined
|
|
|
|
}, function(err) {
|
|
|
|
if (err) {
|
|
|
|
goTo('/login-existing');
|
|
|
|
return;
|
|
|
|
}
|
2014-03-28 13:08:04 -04:00
|
|
|
|
2014-10-20 11:42:46 -04:00
|
|
|
appController._auth.storeCredentials(function(err) {
|
|
|
|
if (err) {
|
|
|
|
return $scope.onError(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
goTo('/desktop');
|
|
|
|
});
|
2014-09-22 08:19:06 -04:00
|
|
|
});
|
2013-10-09 10:40:36 -04:00
|
|
|
}
|
2014-10-02 16:05:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function goTo(location) {
|
|
|
|
$scope.$apply(function() {
|
|
|
|
$location.path(location);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2013-09-15 10:24:14 -04:00
|
|
|
|
2014-10-08 06:34:34 -04:00
|
|
|
module.exports = LoginCtrl;
|