mirror of
https://github.com/moparisthebest/mail
synced 2024-11-22 08:52:15 -05:00
WIP: start refactoring login controllers
This commit is contained in:
parent
bfe827e084
commit
ea74f3443c
@ -1,6 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
var CreateAccountCtrl = function($scope, $location, $routeParams, auth, admin, appConfig) {
|
||||
var CreateAccountCtrl = function($scope, $location, $routeParams, $q, auth, admin, appConfig) {
|
||||
!$routeParams.dev && !auth.isInitialized() && $location.path('/'); // init app
|
||||
|
||||
$scope.createWhiteoutAccount = function() {
|
||||
@ -9,10 +9,14 @@ var CreateAccountCtrl = function($scope, $location, $routeParams, auth, admin, a
|
||||
return;
|
||||
}
|
||||
|
||||
$scope.busy = true;
|
||||
$scope.errMsg = undefined; // reset error msg
|
||||
var emailAddress = $scope.user + '@' + appConfig.config.wmailDomain;
|
||||
|
||||
return $q(function(resolve) {
|
||||
$scope.busy = true;
|
||||
$scope.errMsg = undefined; // reset error msg
|
||||
resolve();
|
||||
|
||||
}).then(function() {
|
||||
// set to state for next view
|
||||
auth.setCredentials({
|
||||
emailAddress: emailAddress,
|
||||
@ -21,23 +25,21 @@ var CreateAccountCtrl = function($scope, $location, $routeParams, auth, admin, a
|
||||
});
|
||||
|
||||
// call REST api
|
||||
admin.createUser({
|
||||
return admin.createUser({
|
||||
emailAddress: emailAddress,
|
||||
password: $scope.pass,
|
||||
phone: $scope.phone.replace(/\s+/g, ''), // remove spaces from the phone number
|
||||
betaCode: $scope.betaCode.toUpperCase()
|
||||
}, function(err) {
|
||||
});
|
||||
|
||||
}).then(function() {
|
||||
$scope.busy = false;
|
||||
|
||||
if (err) {
|
||||
$scope.errMsg = err.errMsg || err.message;
|
||||
$scope.$apply();
|
||||
return;
|
||||
}
|
||||
|
||||
// proceed to login and keygen
|
||||
$location.path('/validate-phone');
|
||||
$scope.$apply();
|
||||
|
||||
}).catch(function(err) {
|
||||
$scope.busy = false;
|
||||
$scope.errMsg = err.errMsg || err.message;
|
||||
});
|
||||
};
|
||||
};
|
||||
|
@ -1,6 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
var LoginExistingCtrl = function($scope, $location, $routeParams, email, auth, keychain) {
|
||||
var LoginExistingCtrl = function($scope, $location, $routeParams, $q, email, auth, keychain) {
|
||||
!$routeParams.dev && !auth.isInitialized() && $location.path('/'); // init app
|
||||
|
||||
$scope.confirmPassphrase = function() {
|
||||
@ -9,50 +9,39 @@ var LoginExistingCtrl = function($scope, $location, $routeParams, email, auth, k
|
||||
return;
|
||||
}
|
||||
|
||||
return $q(function(resolve) {
|
||||
$scope.busy = true;
|
||||
$scope.errMsg = undefined;
|
||||
$scope.incorrect = false;
|
||||
resolve();
|
||||
|
||||
unlockCrypto();
|
||||
};
|
||||
|
||||
function unlockCrypto() {
|
||||
}).then(function() {
|
||||
// key keypair
|
||||
var userId = auth.emailAddress;
|
||||
keychain.getUserKeyPair(userId, function(err, keypair) {
|
||||
if (err) {
|
||||
displayError(err);
|
||||
return;
|
||||
}
|
||||
return keychain.getUserKeyPair(userId);
|
||||
|
||||
email.unlock({
|
||||
}).then(function(keypair) {
|
||||
// unlock email service
|
||||
return email.unlock({
|
||||
keypair: keypair,
|
||||
passphrase: $scope.passphrase
|
||||
}, onUnlock);
|
||||
});
|
||||
}
|
||||
|
||||
function onUnlock(err) {
|
||||
if (err) {
|
||||
displayError(err);
|
||||
return;
|
||||
}
|
||||
|
||||
auth.storeCredentials(function(err) {
|
||||
if (err) {
|
||||
displayError(err);
|
||||
return;
|
||||
}
|
||||
}).then(function() {
|
||||
// persist credentials locally
|
||||
return auth.storeCredentials();
|
||||
|
||||
}).then(function() {
|
||||
// go to main account screen
|
||||
$location.path('/account');
|
||||
$scope.$apply();
|
||||
});
|
||||
}
|
||||
|
||||
}).catch(displayError);
|
||||
};
|
||||
|
||||
function displayError(err) {
|
||||
$scope.busy = false;
|
||||
$scope.incorrect = true;
|
||||
$scope.errMsg = err.errMsg || err.message;
|
||||
$scope.$apply();
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
var LoginInitialCtrl = function($scope, $location, $routeParams, newsletter, email, auth) {
|
||||
var LoginInitialCtrl = function($scope, $location, $routeParams, $q, newsletter, email, auth) {
|
||||
!$routeParams.dev && !auth.isInitialized() && $location.path('/'); // init app
|
||||
|
||||
var emailAddress = auth.emailAddress;
|
||||
@ -51,24 +51,23 @@ var LoginInitialCtrl = function($scope, $location, $routeParams, newsletter, ema
|
||||
// go to set keygen screen
|
||||
$scope.setState(states.PROCESSING);
|
||||
|
||||
email.unlock({
|
||||
passphrase: undefined // generate key without passphrase
|
||||
}, function(err) {
|
||||
if (err) {
|
||||
displayError(err);
|
||||
return;
|
||||
}
|
||||
return $q(function(resolve) {
|
||||
resolve();
|
||||
}).then(function() {
|
||||
// generate key without passphrase
|
||||
return email.unlock({
|
||||
passphrase: undefined
|
||||
});
|
||||
|
||||
auth.storeCredentials(function(err) {
|
||||
if (err) {
|
||||
displayError(err);
|
||||
return;
|
||||
}
|
||||
}).then(function() {
|
||||
// persist credentials locally
|
||||
return auth.storeCredentials();
|
||||
|
||||
}).then(function() {
|
||||
// go to main account screen
|
||||
$location.path('/account');
|
||||
$scope.$apply();
|
||||
});
|
||||
});
|
||||
|
||||
}).catch(displayError);
|
||||
};
|
||||
|
||||
$scope.setState = function(state) {
|
||||
@ -78,7 +77,6 @@ var LoginInitialCtrl = function($scope, $location, $routeParams, newsletter, ema
|
||||
function displayError(err) {
|
||||
$scope.setState(states.IDLE);
|
||||
$scope.errMsg = err.errMsg || err.message;
|
||||
$scope.$apply();
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
var LoginExistingCtrl = function($scope, $location, $routeParams, email, auth, pgp, keychain) {
|
||||
var LoginExistingCtrl = function($scope, $location, $routeParams, $q, email, auth, pgp, keychain) {
|
||||
!$routeParams.dev && !auth.isInitialized() && $location.path('/'); // init app
|
||||
|
||||
$scope.incorrect = false;
|
||||
@ -11,31 +11,28 @@ var LoginExistingCtrl = function($scope, $location, $routeParams, email, auth, p
|
||||
return;
|
||||
}
|
||||
|
||||
var userId = auth.emailAddress,
|
||||
keypair;
|
||||
|
||||
return $q(function(resolve) {
|
||||
$scope.busy = true;
|
||||
$scope.errMsg = undefined; // reset error msg
|
||||
$scope.incorrect = false;
|
||||
resolve();
|
||||
|
||||
unlockCrypto();
|
||||
};
|
||||
|
||||
function unlockCrypto() {
|
||||
var userId = auth.emailAddress;
|
||||
}).then(function() {
|
||||
// check if user already has a public key on the key server
|
||||
keychain.getUserKeyPair(userId, function(err, keypair) {
|
||||
if (err) {
|
||||
$scope.displayError(err);
|
||||
return;
|
||||
}
|
||||
return keychain.getUserKeyPair(userId);
|
||||
|
||||
keypair = keypair || {};
|
||||
}).then(function(keys) {
|
||||
keypair = keys || {};
|
||||
|
||||
// extract public key from private key block if missing in key file
|
||||
if (!$scope.key.publicKeyArmored || $scope.key.publicKeyArmored.indexOf('-----BEGIN PGP PUBLIC KEY BLOCK-----') < 0) {
|
||||
try {
|
||||
$scope.key.publicKeyArmored = pgp.extractPublicKey($scope.key.privateKeyArmored);
|
||||
} catch (e) {
|
||||
$scope.displayError(new Error('Error reading PGP key!'));
|
||||
return;
|
||||
throw new Error('Error reading PGP key!');
|
||||
}
|
||||
}
|
||||
|
||||
@ -45,8 +42,7 @@ var LoginExistingCtrl = function($scope, $location, $routeParams, email, auth, p
|
||||
privKeyParams = pgp.getKeyParams($scope.key.privateKeyArmored);
|
||||
pubKeyParams = pgp.getKeyParams($scope.key.publicKeyArmored);
|
||||
} catch (e) {
|
||||
$scope.displayError(new Error('Error reading key params!'));
|
||||
return;
|
||||
throw new Error('Error reading key paramaters!');
|
||||
}
|
||||
|
||||
// set parsed private key
|
||||
@ -68,43 +64,33 @@ var LoginExistingCtrl = function($scope, $location, $routeParams, email, auth, p
|
||||
}
|
||||
|
||||
// import and validate keypair
|
||||
email.unlock({
|
||||
return email.unlock({
|
||||
keypair: keypair,
|
||||
passphrase: $scope.passphrase
|
||||
}, function(err) {
|
||||
if (err) {
|
||||
}).catch(function(err) {
|
||||
$scope.incorrect = true;
|
||||
$scope.displayError(err);
|
||||
return;
|
||||
}
|
||||
|
||||
keychain.putUserKeyPair(keypair, onUnlock);
|
||||
throw err;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function onUnlock(err) {
|
||||
if (err) {
|
||||
$scope.displayError(err);
|
||||
return;
|
||||
}
|
||||
}).then(function() {
|
||||
// perist keys locally
|
||||
return keychain.putUserKeyPair(keypair);
|
||||
|
||||
auth.storeCredentials(function(err) {
|
||||
if (err) {
|
||||
$scope.displayError(err);
|
||||
return;
|
||||
}
|
||||
}).then(function() {
|
||||
// persist credentials locally
|
||||
return auth.storeCredentials();
|
||||
|
||||
}).then(function() {
|
||||
// go to main account screen
|
||||
$location.path('/account');
|
||||
$scope.$apply();
|
||||
});
|
||||
}
|
||||
|
||||
$scope.displayError = function(err) {
|
||||
}).catch(displayError);
|
||||
};
|
||||
|
||||
function displayError(err) {
|
||||
$scope.busy = false;
|
||||
$scope.errMsg = err.errMsg || err.message;
|
||||
$scope.$apply();
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = LoginExistingCtrl;
|
@ -4,88 +4,63 @@ var LoginCtrl = function($scope, $timeout, $location, updateHandler, account, au
|
||||
|
||||
// check for app update
|
||||
updateHandler.checkForUpdate();
|
||||
|
||||
// initialize the user account
|
||||
initializeUser();
|
||||
|
||||
function initializeUser() {
|
||||
// init the auth modules
|
||||
auth.init(function(err) {
|
||||
if (err) {
|
||||
return dialog.error(err);
|
||||
}
|
||||
|
||||
// get OAuth token from chrome
|
||||
auth.getEmailAddress(function(err, info) {
|
||||
if (err) {
|
||||
dialog.error(err);
|
||||
return;
|
||||
}
|
||||
auth.init().then(function() {
|
||||
// get email address
|
||||
return auth.getEmailAddress();
|
||||
|
||||
}).then(function(info) {
|
||||
// check if account needs to be selected
|
||||
if (!info.emailAddress) {
|
||||
$scope.goTo('/add-account');
|
||||
return;
|
||||
return $scope.goTo('/add-account');
|
||||
}
|
||||
|
||||
// initiate the account by initializing the email dao and user storage
|
||||
account.init({
|
||||
return account.init({
|
||||
emailAddress: info.emailAddress,
|
||||
realname: info.realname
|
||||
}, function(err, availableKeys) {
|
||||
if (err) {
|
||||
dialog.error(err);
|
||||
return;
|
||||
}
|
||||
}).then(function(availableKeys) {
|
||||
return redirect(availableKeys);
|
||||
});
|
||||
|
||||
redirect(availableKeys);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
}).catch(dialog.error);
|
||||
|
||||
function redirect(availableKeys) {
|
||||
if (availableKeys && availableKeys.publicKey && availableKeys.privateKey) {
|
||||
// public and private key available, try empty passphrase
|
||||
email.unlock({
|
||||
return email.unlock({
|
||||
keypair: availableKeys,
|
||||
passphrase: undefined
|
||||
}, function(err) {
|
||||
if (err) {
|
||||
$scope.goTo('/login-existing');
|
||||
return;
|
||||
}
|
||||
|
||||
auth.storeCredentials(function(err) {
|
||||
if (err) {
|
||||
return dialog.error(err);
|
||||
}
|
||||
|
||||
$scope.goTo('/account');
|
||||
}).then(function() {
|
||||
// no passphrase set... go to main screen
|
||||
return auth.storeCredentials().then(function() {
|
||||
return $scope.goTo('/account');
|
||||
});
|
||||
|
||||
}).catch(function() {
|
||||
// passphrase set... ask for passphrase
|
||||
return $scope.goTo('/login-existing');
|
||||
});
|
||||
|
||||
} else if (availableKeys && availableKeys.publicKey && !availableKeys.privateKey) {
|
||||
// check if private key is synced
|
||||
keychain.requestPrivateKeyDownload({
|
||||
return keychain.requestPrivateKeyDownload({
|
||||
userId: availableKeys.publicKey.userId,
|
||||
keyId: availableKeys.publicKey._id,
|
||||
}, function(err, privateKeySynced) {
|
||||
if (err) {
|
||||
dialog.error(err);
|
||||
return;
|
||||
}
|
||||
|
||||
}).then(function(privateKeySynced) {
|
||||
if (privateKeySynced) {
|
||||
// private key is synced, proceed to download
|
||||
$scope.goTo('/login-privatekey-download');
|
||||
return;
|
||||
}
|
||||
|
||||
return $scope.goTo('/login-privatekey-download');
|
||||
} else {
|
||||
// no private key, import key file
|
||||
$scope.goTo('/login-new-device');
|
||||
return $scope.goTo('/login-new-device');
|
||||
}
|
||||
});
|
||||
|
||||
} else {
|
||||
// no public key available, start onboarding process
|
||||
$scope.goTo('/login-initial');
|
||||
return $scope.goTo('/login-initial');
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
var ValidatePhoneCtrl = function($scope, $location, $routeParams, mailConfig, auth, admin) {
|
||||
var ValidatePhoneCtrl = function($scope, $location, $routeParams, $q, mailConfig, auth, admin) {
|
||||
!$routeParams.dev && !auth.isInitialized() && $location.path('/'); // init app
|
||||
|
||||
$scope.validateUser = function() {
|
||||
@ -9,23 +9,25 @@ var ValidatePhoneCtrl = function($scope, $location, $routeParams, mailConfig, au
|
||||
return;
|
||||
}
|
||||
|
||||
return $q(function(resolve) {
|
||||
$scope.busy = true;
|
||||
$scope.errMsg = undefined; // reset error msg
|
||||
resolve();
|
||||
|
||||
}).then(function() {
|
||||
// verify user to REST api
|
||||
admin.validateUser({
|
||||
return admin.validateUser({
|
||||
emailAddress: auth.emailAddress,
|
||||
token: $scope.token.toUpperCase()
|
||||
}, function(err) {
|
||||
if (err) {
|
||||
$scope.busy = false;
|
||||
$scope.errMsg = err.errMsg || err.message;
|
||||
$scope.$apply();
|
||||
return;
|
||||
}
|
||||
});
|
||||
|
||||
}).then(function() {
|
||||
// proceed to login
|
||||
$scope.login();
|
||||
|
||||
}).catch(function(err) {
|
||||
$scope.busy = false;
|
||||
$scope.errMsg = err.errMsg || err.message;
|
||||
});
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user