mail/src/js/controller/login-existing.js

71 lines
1.7 KiB
JavaScript
Raw Normal View History

2014-10-02 16:05:44 -04:00
'use strict';
2013-10-21 07:10:42 -04:00
2014-10-02 16:05:44 -04:00
var appController = require('../app-controller');
2013-10-21 07:10:42 -04:00
2014-10-02 16:05:44 -04:00
var LoginExistingCtrl = function($scope, $location, $routeParams) {
if (!appController._emailDao && !$routeParams.dev) {
$location.path('/'); // init app
return;
}
2014-10-02 16:05:44 -04:00
var emailDao = appController._emailDao;
2013-11-04 08:20:14 -05:00
2014-10-02 16:05:44 -04:00
$scope.buttonEnabled = true;
$scope.incorrect = false;
$scope.change = function() {
2013-10-22 08:59:36 -04:00
$scope.incorrect = false;
2014-10-02 16:05:44 -04:00
};
$scope.confirmPassphrase = function() {
if (!$scope.passphrase) {
return;
}
2014-10-02 16:05:44 -04:00
// disable button once loggin has started
$scope.buttonEnabled = false;
$scope.incorrect = false;
unlockCrypto();
};
2014-10-02 16:05:44 -04:00
function unlockCrypto() {
var userId = emailDao._account.emailAddress;
emailDao._keychain.getUserKeyPair(userId, function(err, keypair) {
if (err) {
handleError(err);
2013-10-21 07:10:42 -04:00
return;
}
2014-10-02 16:05:44 -04:00
emailDao.unlock({
keypair: keypair,
passphrase: $scope.passphrase
}, onUnlock);
});
}
2014-10-02 16:05:44 -04:00
function onUnlock(err) {
if (err) {
$scope.incorrect = true;
$scope.buttonEnabled = true;
$scope.$apply();
return;
2013-11-04 08:20:14 -05:00
}
2013-10-21 07:10:42 -04:00
2014-10-02 16:05:44 -04:00
appController._auth.storeCredentials(function(err) {
2013-11-04 08:20:14 -05:00
if (err) {
2014-10-02 16:05:44 -04:00
return $scope.onError(err);
2013-10-21 07:10:42 -04:00
}
2013-11-04 08:20:14 -05:00
2014-10-02 16:05:44 -04:00
$location.path('/desktop');
$scope.$apply();
});
}
2013-10-21 07:10:42 -04:00
2014-10-02 16:05:44 -04:00
function handleError(err) {
$scope.incorrect = true;
$scope.buttonEnabled = true;
$scope.onError(err);
}
};
2013-10-21 07:10:42 -04:00
2014-10-02 16:05:44 -04:00
exports = LoginExistingCtrl;