2013-09-15 10:24:14 -04:00
|
|
|
define(function(require) {
|
|
|
|
'use strict';
|
|
|
|
|
2013-11-14 14:44:57 -05:00
|
|
|
var appController = require('js/app-controller'),
|
|
|
|
errorUtil = require('js/util/error');
|
2013-09-15 10:24:14 -04:00
|
|
|
|
|
|
|
var LoginCtrl = function($scope, $location) {
|
2013-11-12 14:00:53 -05:00
|
|
|
// global state... inherited to all child scopes
|
|
|
|
$scope.$root.state = {};
|
2013-11-14 14:44:57 -05:00
|
|
|
// attach global error handler
|
|
|
|
errorUtil.attachHandler($scope);
|
2013-11-12 14:00:53 -05:00
|
|
|
|
2013-11-14 14:44:57 -05:00
|
|
|
// check for app update
|
|
|
|
appController.checkForUpdate();
|
2013-11-12 14:00:53 -05:00
|
|
|
|
2013-11-14 14:44:57 -05:00
|
|
|
// start main application controller
|
2013-12-09 13:21:52 -05:00
|
|
|
appController.start({
|
|
|
|
onError: $scope.onError
|
|
|
|
}, function(err) {
|
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;
|
|
|
|
}
|
|
|
|
|
2013-10-21 07:10:42 -04:00
|
|
|
initializeUser();
|
2013-10-09 10:40:36 -04:00
|
|
|
});
|
|
|
|
|
2013-10-21 07:10:42 -04:00
|
|
|
function initializeUser() {
|
2013-10-11 17:02:37 -04:00
|
|
|
// get OAuth token from chrome
|
2014-04-01 07:16:39 -04:00
|
|
|
appController._auth.getEmailAddress(function(err, emailAddress) {
|
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-01-27 12:50:13 -05:00
|
|
|
// check if account needs to be selected
|
|
|
|
if (!emailAddress) {
|
2014-03-28 13:08:04 -04:00
|
|
|
goTo('/add-account');
|
2014-01-27 12:50:13 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-04 08:20:14 -05:00
|
|
|
// initiate controller by creating email dao
|
2013-12-09 13:21:52 -05:00
|
|
|
appController.init({
|
|
|
|
emailAddress: emailAddress
|
|
|
|
}, function(err, availableKeys) {
|
2013-10-11 17:02:37 -04:00
|
|
|
if (err) {
|
2013-11-12 14:00:53 -05:00
|
|
|
$scope.onError(err);
|
2013-10-11 17:02:37 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-03 13:21:50 -05:00
|
|
|
redirect(availableKeys);
|
2013-10-11 17:02:37 -04:00
|
|
|
});
|
2013-09-15 10:24:14 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-10-21 07:10:42 -04:00
|
|
|
function redirect(availableKeys) {
|
|
|
|
// redirect if needed
|
2013-10-22 08:48:38 -04:00
|
|
|
if (typeof availableKeys === 'undefined') {
|
2013-10-21 07:10:42 -04:00
|
|
|
// no public key available, start onboarding process
|
2014-03-28 13:08:04 -04:00
|
|
|
goTo('/login-initial');
|
2013-10-21 07:10:42 -04:00
|
|
|
} else if (!availableKeys.privateKey) {
|
|
|
|
// no private key, import key
|
2014-03-28 13:08:04 -04:00
|
|
|
goTo('/login-new-device');
|
2013-10-21 07:10:42 -04:00
|
|
|
} else {
|
2014-03-28 13:08:04 -04:00
|
|
|
// public and private key available, try empty passphrase
|
|
|
|
appController._emailDao.unlock({
|
|
|
|
keypair: availableKeys,
|
|
|
|
passphrase: undefined
|
|
|
|
}, function(err) {
|
|
|
|
if (err) {
|
|
|
|
goTo('/login-existing');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
goTo('/desktop');
|
|
|
|
});
|
2013-10-21 07:10:42 -04:00
|
|
|
}
|
2014-03-28 13:08:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function goTo(location) {
|
|
|
|
$location.path(location);
|
2013-10-09 10:40:36 -04:00
|
|
|
$scope.$apply();
|
|
|
|
}
|
2013-09-15 10:24:14 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
return LoginCtrl;
|
|
|
|
});
|