client-ui
Version:
Testing implementation of nodeJs Backend, angular frontend, and hopefully in a way that this can be deployed to s3/cloudfront
112 lines (99 loc) • 4.08 kB
JavaScript
(function () {
'use strict';
angular.module(moduleName).controller('loginController', loginController);
loginController.$inject = ['$state', '$stateParams', 'AuthStateFactory', 'loan', 'client'];
function loginController($state, $stateParams, AuthStateFactory, loanService, client) {
var self = this;
self.loading = true;
self.init = init;
self.redirect = redirect;
self.goToAfterLogin;
init();
function init() {
resetErrors();
if ($stateParams.hasOwnProperty("forceLogout") && $stateParams.forceLogout === "true") {
AuthStateFactory.logout()
.then(function(success) {
self.logoutError = false;
}, function(error) {
self.logoutError = error;
});
} else {
AuthStateFactory.check();
var redirecting = self.redirect();
}
if (!redirecting) {
self.loading = false;
}
if($stateParams.goTo){
if ($stateParams.goTo === "myDashboard") {
self.goToAfterLogin = "loanDashboard.myDashboard";
} else {
self.goToAfterLogin = $stateParams.goTo;
}
}
}
function redirect() {
if (AuthStateFactory.isLoggedIn) {
if (hasGoNext()) {
client.getClient().then(function(clientData) {
$state.go(loanService.getNextStep());
}, function(error) {
$state.go(loanService.getNextStep());
});
} else {
$state.go('loanDashboard.myDashboard');
}
return true;
}
return false;
};
function hasGoNext() {
return $stateParams.hasOwnProperty('gonext') && $stateParams.gonext === 'true';
};
self.submit = function () {
if(self.loginForm.$invalid){
return;
}
resetErrors();
self.working = true;
AuthStateFactory.login(self.credentials)
.then(function (response) {
return loanService.getLoansByClient(response.clientId).then(function () {
self.working = false;
if(self.goToAfterLogin){
$state.go(self.goToAfterLogin);
} else {
$state.go(loanService.getNextStep());
}
}, function () {
self.working = false;
});
}, function (err) {
self.working = false;
self.errorCode = err.code || "UNKNOWN_ERROR";
// artificially high default to supress warnings if this prop is not returned from login api
self.attemptsRemaining = err.attemptsRemaining || '999';
});
};
self.requestPasswordReset = function () {
resetErrors();
if (self.loginForm.email.$invalid) {
self.passwordResetMessage = 'Please enter your email address';
return;
}
AuthStateFactory.requestPasswordReset(self.credentials.email)
.then(function (res) {
self.passwordResetRequested = true;
}, function (err) {
self.passwordResetMessage = 'An error occurred in our system. We are unable to reset your password at this time. Please try again later.';
});
};
function resetErrors() {
self.logoutError = false;
self.passwordResetRequested = false;
self.passwordResetMessage = '';
self.errorCode = false;
}
}
})();