Refactor all login controllers to promises

This commit is contained in:
Tankred Hase 2014-12-17 13:32:12 +01:00
parent ea74f3443c
commit 085b104521
5 changed files with 82 additions and 95 deletions

View File

@ -1,6 +1,6 @@
'use strict';
var AddAccountCtrl = function($scope, $location, $routeParams, mailConfig, auth, dialog) {
var AddAccountCtrl = function($scope, $location, $routeParams, $timeout, $q, mailConfig, auth, dialog) {
!$routeParams.dev && !auth.isInitialized() && $location.path('/'); // init app
$scope.getAccountSettings = function() {
@ -9,10 +9,15 @@ var AddAccountCtrl = function($scope, $location, $routeParams, mailConfig, auth,
return;
}
$scope.busy = true;
$scope.errMsg = undefined; // reset error msg
return $q(function(resolve) {
$scope.busy = true;
$scope.errMsg = undefined; // reset error msg
resolve();
return mailConfig.get($scope.emailAddress).then(function(config) {
}).then(function() {
return mailConfig.get($scope.emailAddress);
}).then(function(config) {
$scope.busy = false;
$scope.state.login = {
mailConfig: config,
@ -22,10 +27,10 @@ var AddAccountCtrl = function($scope, $location, $routeParams, mailConfig, auth,
var hostname = config.imap.hostname;
if (auth.useOAuth(hostname)) {
// check for oauth support
$scope.oauthPossible();
return $scope.oauthPossible();
} else {
// use standard password login
$scope.setCredentials();
return $scope.setCredentials();
}
}).catch(function() {
@ -36,7 +41,7 @@ var AddAccountCtrl = function($scope, $location, $routeParams, mailConfig, auth,
$scope.oauthPossible = function() {
// ask user to use the platform's native OAuth api
dialog.confirm({
return dialog.confirm({
title: 'Google Account Login',
message: 'You are signing into a Google account. Would you like to sign in with Google or just continue with a password login?',
positiveBtnStr: 'Google sign in',
@ -50,25 +55,24 @@ var AddAccountCtrl = function($scope, $location, $routeParams, mailConfig, auth,
} else {
// use normal user/password login
$scope.setCredentials();
$scope.$apply();
}
}
});
function getOAuthToken() {
// fetches the email address from the chrome identity api
auth.getOAuthToken(function(err) {
if (err) {
return dialog.error(err);
}
$scope.setCredentials();
$scope.$apply();
});
auth.getOAuthToken().then(function() {
// continue to setting credentials
return $scope.setCredentials();
}).catch(dialog.error);
}
};
$scope.setCredentials = function() {
$location.path('/login-set-credentials');
return $timeout(function() {
$location.path('/login-set-credentials');
});
};
};

View File

@ -44,15 +44,15 @@ var LoginInitialCtrl = function($scope, $location, $routeParams, $q, newsletter,
return;
}
$scope.errMsg = undefined;
// sing up to newsletter
newsletter.signup(emailAddress, $scope.newsletter);
// go to set keygen screen
$scope.setState(states.PROCESSING);
return $q(function(resolve) {
$scope.errMsg = undefined;
resolve();
}).then(function() {
// generate key without passphrase
return email.unlock({

View File

@ -1,6 +1,6 @@
'use strict';
var LoginPrivateKeyDownloadCtrl = function($scope, $location, $routeParams, auth, email, keychain) {
var LoginPrivateKeyDownloadCtrl = function($scope, $location, $routeParams, $q, auth, email, keychain) {
!$routeParams.dev && !auth.isInitialized() && $location.path('/'); // init app
$scope.step = 1;
@ -15,42 +15,32 @@ var LoginPrivateKeyDownloadCtrl = function($scope, $location, $routeParams, auth
return;
}
$scope.busy = true;
$scope.errMsg = undefined;
$scope.verifyRecoveryToken(function() {
$scope.busy = false;
$scope.errMsg = undefined;
$scope.step++;
$scope.$apply();
});
};
$scope.verifyRecoveryToken = function(callback) {
var userId = auth.emailAddress;
keychain.getUserKeyPair(userId, function(err, keypair) {
if (err) {
displayError(err);
return;
}
return $q(function(resolve) {
$scope.busy = true;
$scope.errMsg = undefined;
resolve();
}).then(function() {
// get public key id for reference
return keychain.getUserKeyPair(userId);
}).then(function(keypair) {
// remember for storage later
$scope.cachedKeypair = keypair;
keychain.downloadPrivateKey({
return keychain.downloadPrivateKey({
userId: userId,
keyId: keypair.publicKey._id,
recoveryToken: $scope.recoveryToken.toUpperCase()
}, function(err, encryptedPrivateKey) {
if (err) {
displayError(err);
return;
}
$scope.encryptedPrivateKey = encryptedPrivateKey;
callback();
});
});
}).then(function(encryptedPrivateKey) {
$scope.encryptedPrivateKey = encryptedPrivateKey;
$scope.busy = false;
$scope.step++;
}).catch(displayError);
};
//
@ -63,47 +53,39 @@ var LoginPrivateKeyDownloadCtrl = function($scope, $location, $routeParams, auth
return;
}
$scope.busy = true;
$scope.errMsg = undefined;
$scope.decryptAndStorePrivateKeyLocally();
};
$scope.decryptAndStorePrivateKeyLocally = function() {
var options = $scope.encryptedPrivateKey;
options.code = $scope.code.toUpperCase();
keychain.decryptAndStorePrivateKeyLocally(options, function(err, privateKey) {
if (err) {
displayError(err);
return;
}
return $q(function(resolve) {
$scope.busy = true;
$scope.errMsg = undefined;
resolve();
}).then(function() {
return keychain.decryptAndStorePrivateKeyLocally(options);
}).then(function(privateKey) {
// add private key to cached keypair object
$scope.cachedKeypair.privateKey = privateKey;
// try empty passphrase
email.unlock({
return email.unlock({
keypair: $scope.cachedKeypair,
passphrase: undefined
}, function(err) {
if (err) {
// go to passphrase login screen
$scope.goTo('/login-existing');
return;
}
// passphrase is corrent ... go to main app
auth.storeCredentials(function(err) {
if (err) {
displayError(err);
return;
}
$scope.goTo('/account');
});
}).catch(function(err) {
// passphrase incorrct ... go to passphrase login screen
$scope.goTo('/login-existing');
throw err;
});
});
}).then(function() {
// passphrase is corrent ...
return auth.storeCredentials();
}).then(function() {
// continue to main app
$scope.goTo('/account');
}).catch(displayError);
};
//
@ -112,14 +94,12 @@ var LoginPrivateKeyDownloadCtrl = function($scope, $location, $routeParams, auth
$scope.goTo = function(location) {
$location.path(location);
$scope.$apply();
};
function displayError(err) {
$scope.busy = false;
$scope.incorrect = true;
$scope.errMsg = err.errMsg || err.message;
$scope.$apply();
}
};

View File

@ -4,7 +4,7 @@ var ENCRYPTION_METHOD_NONE = 0;
var ENCRYPTION_METHOD_STARTTLS = 1;
var ENCRYPTION_METHOD_TLS = 2;
var SetCredentialsCtrl = function($scope, $location, $routeParams, auth, connectionDoctor) {
var SetCredentialsCtrl = function($scope, $location, $routeParams, $q, auth, connectionDoctor) {
!$routeParams.dev && !auth.isInitialized() && $location.path('/'); // init app
//
@ -82,19 +82,23 @@ var SetCredentialsCtrl = function($scope, $location, $routeParams, auth, connect
connectionDoctor.configure(credentials);
// run connection doctor test suite
$scope.busy = true;
connectionDoctor.check(function(err) {
if (err) {
// display the error in the settings UI
$scope.connectionError = err;
} else {
// persists the credentials and forwards to /login
auth.setCredentials(credentials);
$location.path('/login');
}
return $q(function(resolve) {
$scope.busy = true;
resolve();
}).then(function() {
return connectionDoctor.check();
}).then(function() {
// persists the credentials and forwards to /login
auth.setCredentials(credentials);
$scope.busy = false;
$location.path('/login');
}).catch(function(err) {
// display the error in the settings UI
$scope.connectionError = err;
$scope.busy = false;
$scope.$apply();
});
};
};

View File

@ -23,7 +23,7 @@ var ValidatePhoneCtrl = function($scope, $location, $routeParams, $q, mailConfig
}).then(function() {
// proceed to login
$scope.login();
return $scope.login();
}).catch(function(err) {
$scope.busy = false;
@ -56,8 +56,7 @@ var ValidatePhoneCtrl = function($scope, $location, $routeParams, $q, mailConfig
$location.path('/login');
}).catch(function() {
$scope.busy = false;
$scope.errMsg = 'Error fetching IMAP settings!';
throw new Error('Error fetching IMAP settings!');
});
};
};