2013-09-15 10:24:14 -04:00
|
|
|
define(function(require) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var appController = require('js/app-controller');
|
|
|
|
|
|
|
|
var LoginCtrl = function($scope, $location) {
|
2013-10-09 10:40:36 -04:00
|
|
|
appController.start(function(err) {
|
|
|
|
if (err) {
|
|
|
|
console.error(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-21 07:10:42 -04:00
|
|
|
if (!window.chrome || !chrome.identity) {
|
|
|
|
$location.path('/desktop');
|
|
|
|
$scope.$apply();
|
2013-10-09 10:40:36 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-08 13:08:34 -05:00
|
|
|
// login to imap
|
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
|
2013-10-21 07:10:42 -04:00
|
|
|
appController.fetchOAuthToken(function(err, auth) {
|
2013-09-15 10:24:14 -04:00
|
|
|
if (err) {
|
2013-09-26 11:37:56 -04:00
|
|
|
console.error(err);
|
2013-09-15 10:24:14 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-04 08:20:14 -05:00
|
|
|
// initiate controller by creating email dao
|
2013-10-21 07:10:42 -04:00
|
|
|
appController.init(auth.emailAddress, auth.token, function(err, availableKeys) {
|
2013-10-11 17:02:37 -04:00
|
|
|
if (err) {
|
|
|
|
console.error(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-04 08:20:14 -05:00
|
|
|
// login to imap backend
|
|
|
|
appController._emailDao.imapLogin(function(err) {
|
|
|
|
if (err) {
|
|
|
|
console.error(err);
|
|
|
|
console.log('Error logging into IMAP... proceeding in offline mode.');
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
$location.path('/login-initial');
|
|
|
|
} else if (!availableKeys.privateKey) {
|
|
|
|
// no private key, import key
|
|
|
|
$location.path('/login-new-device');
|
|
|
|
} else {
|
|
|
|
// public and private key available, just login
|
|
|
|
$location.path('/login-existing');
|
|
|
|
}
|
2013-10-09 10:40:36 -04:00
|
|
|
$scope.$apply();
|
|
|
|
}
|
2013-09-15 10:24:14 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
return LoginCtrl;
|
|
|
|
});
|