Advertisement
Guest User

Untitled

a guest
Feb 17th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.05 KB | None | 0 0
  1. angular.module("main")
  2.  
  3. .controller("MainCtrl", ["$localStorage", "$scope", "$state", "API", "LoginService", "User", function(ls, $scope, $state, API, LoginService, User) {
  4. var vm = this;
  5. var LS_USERNAME = "user.username";
  6.  
  7. vm.showSignOut = false;
  8.  
  9. $scope.$on("$stateChangeSuccess", updateSignOut);
  10. $scope.$on("authUpdate", updateSignOut);
  11.  
  12. // show Reset Password popup (step 2) if query params contain token and tokenType
  13. // only at /NewPassword
  14. $scope.$on("$stateChangeSuccess", function() {
  15. var params = $state.params;
  16.  
  17. // open Reset Password popup only at /NewPassword
  18. if($state.current.name !== "main.new-password") return;
  19.  
  20. if(params.token && params.tokenType) {
  21. User.auth = {
  22. AccessToken: params.token,
  23. TokenType: params.tokenType
  24. };
  25.  
  26. User.storeAuth();
  27.  
  28. $scope.resetPassword.show();
  29. $scope.resetPassword.step = 2;
  30.  
  31. $scope.resetPassword.loading = true;
  32. API.Account.ResetPassword.Get().finally(function() {
  33. $scope.resetPassword.loading = false;
  34. }).catch($scope.resetPassword.close);
  35. }
  36. });
  37.  
  38. vm.logout = function() {
  39. User.clearStorage();
  40. $state.transitionTo("main.home");
  41. };
  42.  
  43. vm.signup = function() {
  44. User.clearStorage();
  45. $state.transitionTo("main.register");
  46. };
  47.  
  48. // Login
  49.  
  50. vm.loginPopup = (function() {
  51. var self = {
  52. show: show,
  53. close: close,
  54.  
  55. login: login
  56. };
  57.  
  58. resetFormModel();
  59. resetFormErrors();
  60.  
  61. function resetFormModel() {
  62. self.formModel = {
  63. UserName: ls[LS_USERNAME],
  64. Password: null
  65. };
  66.  
  67. self.rememberUsername = !!self.formModel.UserName;
  68. }
  69.  
  70. function resetFormErrors() {
  71. if(self.form) {
  72. self.form.$setPristine();
  73. self.form.$setUntouched();
  74. }
  75.  
  76. self.authError = null;
  77. }
  78.  
  79. function login() {
  80. resetFormErrors();
  81.  
  82. self.loading = true;
  83. LoginService.login(self.formModel).finally(function() {
  84. self.loading = false;
  85. }).then(function(auth) {
  86. if(self.rememberUsername)
  87. ls[LS_USERNAME] = self.formModel.UserName;
  88.  
  89. resetFormModel();
  90. close();
  91.  
  92. // redirect user according to Phase
  93. User.setPhase(auth.Phase);
  94. }).catch(function(error) {
  95. if(error.type === "logic" && error.data) {
  96. return dialogs.message.show({
  97. msg: "Your code is incorrect. Please contact CustomerService@ef.biz",
  98. type: "Error",
  99. buttons: {
  100. ok: {
  101. title: "OK",
  102. handler: dialogs.message.close
  103. }
  104. }
  105. });
  106. }
  107.  
  108. self.authError = error.message;
  109. });
  110. }
  111.  
  112. function show() { self.visible = true; }
  113. function close() { self.visible = false; }
  114.  
  115. return self;
  116. })();
  117.  
  118. // show Sign Out instead of Login button if user already signed in and opened registration
  119. function updateSignOut() {
  120. vm.showSignOut = $state.current.name === "main.register" && User.isAuthorized();
  121. }
  122.  
  123. // Reset Password
  124.  
  125. $scope.resetPassword = {
  126. show: function() {
  127. vm.loginPopup.close();
  128. this.visible = true;
  129. this.step = 1;
  130.  
  131. // clear forms
  132. this.Email = null;
  133. this.NewPassword = null;
  134. this.ConfirmPassword = null;
  135. this.Token = null;
  136. this.NumberOfRetries = null;
  137. $scope.form1.$setUntouched();
  138. $scope.form2.$setUntouched();
  139. },
  140.  
  141. close: function() {
  142. vm.loginPopup.show();
  143. this.visible = false;
  144. },
  145.  
  146. sendMail: function() {
  147. // todo: message before call
  148.  
  149. dialogs.message.show({
  150. msg: "You will receive password reset instructions email and security token to the registered cell phone",
  151. type: "Info",
  152. buttons: {
  153. ok: {
  154. title: "OK",
  155. handler: dialogs.message.close
  156. }
  157. },
  158. onclose: sendMail
  159. });
  160.  
  161. function sendMail() {
  162. $scope.resetPassword.loading = true;
  163. API.User.ForgotPassword({
  164. EmailAddress: $scope.resetPassword.Email
  165. }).finally(function() {
  166. $scope.resetPassword.loading = false;
  167. }).then(function() {
  168. $scope.resetPassword.close();
  169.  
  170. dialogs.notification.show({
  171. msg: "Mail was sent"
  172. });
  173. }).catch(function(error) {
  174. if(error.data && error.type === "validation") {
  175. $scope.form1.email.$server.errors = error.data["email.EmailAddress"] || null;
  176. $scope.form1.email.$setValidity("server", false);
  177. }
  178.  
  179. if(!error.data) {
  180. dialogs.notification.show({
  181. msg: error.message,
  182. type: "Error"
  183. });
  184. }
  185. });
  186. }
  187. },
  188.  
  189. changePassword: function() {
  190. $scope.resetPassword.loading = true;
  191. API.Account.ResetPasswordAndLogin($scope.resetPassword).finally(function() {
  192. $scope.resetPassword.loading = false;
  193. }).then(function(auth) {
  194. $scope.resetPassword.close();
  195. vm.loginPopup.close();
  196.  
  197. LoginService.loginByAuth(auth);
  198.  
  199. // redirect user according to Phase
  200. User.setPhase(auth.Phase);
  201. }).catch(function(error) {
  202. error.data.NumberOfRetries = 1;
  203. if(error.data && error.data.NumberOfRetries) {
  204. $scope.resetPassword.NumberOfRetries = error.data.NumberOfRetries;
  205.  
  206. if($scope.resetPassword.NumberOfRetries == 0) {
  207. API.Account.GetCustomerServicePhone().then(function(customerServicePhone) {
  208. dialogs.message.show({
  209. msg: "Invalid Access Token, Please Contact EF Customer Service: " + customerServicePhone,
  210. type: "Error",
  211. buttons: {
  212. ok: {
  213. title: "OK",
  214. handler: dialogs.message.close
  215. }
  216. },
  217. onclose: function() {
  218. window.location.href = "/";
  219. }
  220. });
  221. });
  222. }
  223. }
  224. });
  225. }
  226. };
  227.  
  228. // Login Help
  229.  
  230. $scope.loginHelp = {
  231. show: function() {
  232. vm.loginPopup.close();
  233. this.visible = true;
  234.  
  235. $scope.loginHelp.loading = true;
  236. API.Account.GetCustomerServicePhone().finally(function() {
  237. $scope.loginHelp.loading = false;
  238. }).then(function(customerServicePhone) {
  239. $scope.loginHelp.customerServicePhone = customerServicePhone;
  240. });
  241. },
  242.  
  243. close: function() {
  244. vm.loginPopup.show();
  245. this.visible = false;
  246. this.showMessageSent = false;
  247. },
  248.  
  249. sendMessage: function() {
  250. $scope.loginHelp.loading = true;
  251. API.Account.SendMessage($scope.loginHelp).finally(function() {
  252. $scope.loginHelp.loading = false;
  253. }).then(function(result) {
  254. if(result === true) {
  255. $scope.loginHelp.Message = null;
  256. $scope.loginHelp.showMessageSent = true;
  257. }
  258. });
  259. }
  260. };
  261. }]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement