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