Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Controller for user Accounts |
| 3 | * |
| 4 | * @module app/users |
| 5 | * @exports userAccountsController |
| 6 | * @name userAccountsController |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | window.angular && (function (angular) { |
| 10 | 'use strict'; |
| 11 | |
| 12 | angular |
| 13 | .module('app.users') |
| 14 | .controller('userAccountsController', [ |
Gunnar Mills | eedefd3 | 2018-02-28 17:02:34 -0600 | [diff] [blame] | 15 | '$scope', |
| 16 | '$window', |
| 17 | 'APIUtils', |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 18 | 'dataService', |
| 19 | function($scope, $window, APIUtils, dataService){ |
| 20 | $scope.dataService = dataService; |
Gunnar Mills | 08744f8 | 2018-03-19 16:24:41 -0500 | [diff] [blame] | 21 | $scope.state = "none"; |
| 22 | $scope.errorMsg = ""; |
| 23 | |
Gunnar Mills | 6b97dde | 2018-03-14 17:04:58 -0500 | [diff] [blame] | 24 | $scope.changePassword = function(oldPassword, newPassword, confirmNewPassword){ |
Gunnar Mills | 08744f8 | 2018-03-19 16:24:41 -0500 | [diff] [blame] | 25 | var user = $scope.dataService.getUser(); |
Gunnar Mills | 6b97dde | 2018-03-14 17:04:58 -0500 | [diff] [blame] | 26 | if(!oldPassword || !newPassword || !confirmNewPassword ){ |
Gunnar Mills | 08744f8 | 2018-03-19 16:24:41 -0500 | [diff] [blame] | 27 | $scope.state = "error"; |
| 28 | $scope.errorMsg = "Field is required!"; |
Gunnar Mills | 6b97dde | 2018-03-14 17:04:58 -0500 | [diff] [blame] | 29 | return false; |
| 30 | } |
| 31 | if (newPassword !== confirmNewPassword){ |
Gunnar Mills | 08744f8 | 2018-03-19 16:24:41 -0500 | [diff] [blame] | 32 | $scope.state = "error"; |
| 33 | $scope.errorMsg = "New passwords do not match!"; |
Gunnar Mills | 6b97dde | 2018-03-14 17:04:58 -0500 | [diff] [blame] | 34 | return false; |
| 35 | } |
| 36 | if (newPassword === oldPassword){ |
Gunnar Mills | 08744f8 | 2018-03-19 16:24:41 -0500 | [diff] [blame] | 37 | $scope.state = "error"; |
| 38 | $scope.errorMsg = "New password and old password match!"; |
Gunnar Mills | 6b97dde | 2018-03-14 17:04:58 -0500 | [diff] [blame] | 39 | return false; |
| 40 | } |
Gunnar Mills | 6b97dde | 2018-03-14 17:04:58 -0500 | [diff] [blame] | 41 | |
Gunnar Mills | 32581cf | 2018-03-16 15:52:54 -0500 | [diff] [blame] | 42 | // Verify the oldPassword is correct |
Gunnar Mills | 08744f8 | 2018-03-19 16:24:41 -0500 | [diff] [blame] | 43 | APIUtils.testPassword(user, oldPassword).then(function(state){ |
| 44 | APIUtils.changePassword(user, newPassword).then(function(response){ |
Gunnar Mills | 32581cf | 2018-03-16 15:52:54 -0500 | [diff] [blame] | 45 | // Clear the textboxes on a success |
| 46 | $scope.passwordVerify = ''; |
| 47 | $scope.password = ''; |
| 48 | $scope.oldPassword = ''; |
Gunnar Mills | 08744f8 | 2018-03-19 16:24:41 -0500 | [diff] [blame] | 49 | $scope.state = "success"; |
Gunnar Mills | 32581cf | 2018-03-16 15:52:54 -0500 | [diff] [blame] | 50 | }, function(error){ |
Gunnar Mills | 08744f8 | 2018-03-19 16:24:41 -0500 | [diff] [blame] | 51 | $scope.state = "error"; |
| 52 | $scope.errorMsg = "Error changing password!"; |
Gunnar Mills | 32581cf | 2018-03-16 15:52:54 -0500 | [diff] [blame] | 53 | }); |
Gunnar Mills | 6b97dde | 2018-03-14 17:04:58 -0500 | [diff] [blame] | 54 | }, function(error){ |
Gunnar Mills | 08744f8 | 2018-03-19 16:24:41 -0500 | [diff] [blame] | 55 | $scope.state = "error"; |
| 56 | $scope.errorMsg = "Old password is not correct!"; |
Gunnar Mills | 6b97dde | 2018-03-14 17:04:58 -0500 | [diff] [blame] | 57 | }); |
| 58 | } |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 59 | } |
| 60 | ] |
| 61 | ); |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 62 | })(angular); |