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