1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-11 19:55:06 -05:00
mail/src/js/controller/login.js

87 lines
2.5 KiB
JavaScript
Raw Normal View History

define(function(require) {
'use strict';
var appController = require('js/app-controller'),
errorUtil = require('js/util/error');
var LoginCtrl = function($scope, $location) {
// global state... inherited to all child scopes
$scope.$root.state = {};
// attach global error handler
errorUtil.attachHandler($scope);
// check for app update
appController.checkForUpdate();
// start main application controller
appController.start({
onError: $scope.onError
}, function(err) {
if (err) {
$scope.onError(err);
return;
}
2013-10-21 07:10:42 -04:00
initializeUser();
});
2013-10-21 07:10:42 -04:00
function initializeUser() {
2013-10-11 17:02:37 -04:00
// get OAuth token from chrome
appController._auth.getEmailAddress(function(err, emailAddress) {
if (err) {
$scope.onError(err);
return;
}
2014-01-27 12:50:13 -05:00
// check if account needs to be selected
if (!emailAddress) {
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
appController.init({
emailAddress: emailAddress
}, function(err, availableKeys) {
2013-10-11 17:02:37 -04:00
if (err) {
$scope.onError(err);
2013-10-11 17:02:37 -04:00
return;
}
redirect(availableKeys);
2013-10-11 17:02:37 -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
goTo('/login-initial');
2013-10-21 07:10:42 -04:00
} else if (!availableKeys.privateKey) {
// no private key, import key
goTo('/login-new-device');
2013-10-21 07:10:42 -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;
}
goTo('/desktop');
});
2013-10-21 07:10:42 -04:00
}
}
function goTo(location) {
$location.path(location);
$scope.$apply();
}
};
return LoginCtrl;
});