Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- angular.module("main")
- .controller("MainCtrl", ["$localStorage", "$scope", "$state", "API", "LoginService", "User", function(ls, $scope, $state, API, LoginService, User) {
- var vm = this;
- var LS_USERNAME = "user.username";
- vm.showSignOut = false;
- $scope.$on("$stateChangeSuccess", updateSignOut);
- $scope.$on("authUpdate", updateSignOut);
- // show Reset Password popup (step 2) if query params contain token and tokenType
- // only at /NewPassword
- $scope.$on("$stateChangeSuccess", function() {
- var params = $state.params;
- // open Reset Password popup only at /NewPassword
- if($state.current.name !== "main.new-password") return;
- if(params.token && params.tokenType) {
- User.auth = {
- AccessToken: params.token,
- TokenType: params.tokenType
- };
- User.storeAuth();
- $scope.resetPassword.show();
- $scope.resetPassword.step = 2;
- $scope.resetPassword.loading = true;
- API.Account.ResetPassword.Get().finally(function() {
- $scope.resetPassword.loading = false;
- }).catch($scope.resetPassword.close);
- }
- });
- vm.logout = function() {
- User.clearStorage();
- $state.transitionTo("main.home");
- };
- vm.signup = function() {
- User.clearStorage();
- $state.transitionTo("main.register");
- };
- // Login
- vm.loginPopup = (function() {
- var self = {
- show: show,
- close: close,
- login: login
- };
- resetFormModel();
- resetFormErrors();
- function resetFormModel() {
- self.formModel = {
- UserName: ls[LS_USERNAME],
- Password: null
- };
- self.rememberUsername = !!self.formModel.UserName;
- }
- function resetFormErrors() {
- if(self.form) {
- self.form.$setPristine();
- self.form.$setUntouched();
- }
- self.authError = null;
- }
- function login() {
- resetFormErrors();
- self.loading = true;
- LoginService.login(self.formModel).finally(function() {
- self.loading = false;
- }).then(function(auth) {
- if(self.rememberUsername)
- ls[LS_USERNAME] = self.formModel.UserName;
- resetFormModel();
- close();
- // redirect user according to Phase
- User.setPhase(auth.Phase);
- }).catch(function(error) {
- if(error.type === "logic" && error.data) {
- return dialogs.message.show({
- msg: "Your code is incorrect. Please contact CustomerService@ef.biz",
- type: "Error",
- buttons: {
- ok: {
- title: "OK",
- handler: dialogs.message.close
- }
- }
- });
- }
- self.authError = error.message;
- });
- }
- function show() { self.visible = true; }
- function close() { self.visible = false; }
- return self;
- })();
- // show Sign Out instead of Login button if user already signed in and opened registration
- function updateSignOut() {
- vm.showSignOut = $state.current.name === "main.register" && User.isAuthorized();
- }
- // Reset Password
- $scope.resetPassword = {
- show: function() {
- vm.loginPopup.close();
- this.visible = true;
- this.step = 1;
- // clear forms
- this.Email = null;
- this.NewPassword = null;
- this.ConfirmPassword = null;
- this.Token = null;
- this.NumberOfRetries = null;
- $scope.form1.$setUntouched();
- $scope.form2.$setUntouched();
- },
- close: function() {
- vm.loginPopup.show();
- this.visible = false;
- },
- sendMail: function() {
- // todo: message before call
- dialogs.message.show({
- msg: "You will receive password reset instructions email and security token to the registered cell phone",
- type: "Info",
- buttons: {
- ok: {
- title: "OK",
- handler: dialogs.message.close
- }
- },
- onclose: sendMail
- });
- function sendMail() {
- $scope.resetPassword.loading = true;
- API.User.ForgotPassword({
- EmailAddress: $scope.resetPassword.Email
- }).finally(function() {
- $scope.resetPassword.loading = false;
- }).then(function() {
- $scope.resetPassword.close();
- dialogs.notification.show({
- msg: "Mail was sent"
- });
- }).catch(function(error) {
- if(error.data && error.type === "validation") {
- $scope.form1.email.$server.errors = error.data["email.EmailAddress"] || null;
- $scope.form1.email.$setValidity("server", false);
- }
- if(!error.data) {
- dialogs.notification.show({
- msg: error.message,
- type: "Error"
- });
- }
- });
- }
- },
- changePassword: function() {
- $scope.resetPassword.loading = true;
- API.Account.ResetPasswordAndLogin($scope.resetPassword).finally(function() {
- $scope.resetPassword.loading = false;
- }).then(function(auth) {
- $scope.resetPassword.close();
- vm.loginPopup.close();
- LoginService.loginByAuth(auth);
- // redirect user according to Phase
- User.setPhase(auth.Phase);
- }).catch(function(error) {
- error.data.NumberOfRetries = 1;
- if(error.data && error.data.NumberOfRetries) {
- $scope.resetPassword.NumberOfRetries = error.data.NumberOfRetries;
- if($scope.resetPassword.NumberOfRetries == 0) {
- API.Account.GetCustomerServicePhone().then(function(customerServicePhone) {
- dialogs.message.show({
- msg: "Invalid Access Token, Please Contact EF Customer Service: " + customerServicePhone,
- type: "Error",
- buttons: {
- ok: {
- title: "OK",
- handler: dialogs.message.close
- }
- },
- onclose: function() {
- window.location.href = "/";
- }
- });
- });
- }
- }
- });
- }
- };
- // Login Help
- $scope.loginHelp = {
- show: function() {
- vm.loginPopup.close();
- this.visible = true;
- $scope.loginHelp.loading = true;
- API.Account.GetCustomerServicePhone().finally(function() {
- $scope.loginHelp.loading = false;
- }).then(function(customerServicePhone) {
- $scope.loginHelp.customerServicePhone = customerServicePhone;
- });
- },
- close: function() {
- vm.loginPopup.show();
- this.visible = false;
- this.showMessageSent = false;
- },
- sendMessage: function() {
- $scope.loginHelp.loading = true;
- API.Account.SendMessage($scope.loginHelp).finally(function() {
- $scope.loginHelp.loading = false;
- }).then(function(result) {
- if(result === true) {
- $scope.loginHelp.Message = null;
- $scope.loginHelp.showMessageSent = true;
- }
- });
- }
- };
- }]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement