|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 | 3 | angular.module('<%= scriptAppName %>')
|
4 |
| - .controller('SettingsCtrl', function ($scope, User, Auth) { |
| 4 | + .controller('SettingsCtrl', function ($scope, Auth) { |
5 | 5 | $scope.errors = {};
|
6 | 6 |
|
7 |
| - $scope.changePassword = function(form) { |
8 |
| - $scope.submitted = true; |
9 |
| - if(form.$valid) { |
| 7 | + $scope.user = Auth.getCurrentUser(); |
| 8 | + $scope.email = {}; |
| 9 | + |
| 10 | + var getEmail = function(user) { |
| 11 | + if (!$scope.user.credentials.length) { |
| 12 | + return null; |
| 13 | + } |
| 14 | + |
| 15 | + for(var i in $scope.user.credentials) { |
| 16 | + var c = $scope.user.credentials[i]; |
| 17 | + if(c.type==='email') return [c.value, c.confirmed]; |
| 18 | + } |
| 19 | + }; |
| 20 | + |
| 21 | + var tmp = getEmail($scope.user); |
| 22 | + |
| 23 | + var initialEmail = tmp ? tmp[0] : null; |
| 24 | + $scope.email.confirmed = tmp ? tmp[1] : null; |
| 25 | + |
| 26 | + $scope.user.email = initialEmail; |
| 27 | + |
| 28 | + $scope.changeEmail = function () { |
| 29 | + if($scope.email.$valid) { |
| 30 | + Auth.changeEmail(initialEmail, $scope.user.email) |
| 31 | + .then(function() { |
| 32 | + $scope.message = 'Email successfully changed'; |
| 33 | + }) |
| 34 | + .catch(function() { |
| 35 | + // TODO: handle errors |
| 36 | + $scope.message = ''; |
| 37 | + }); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + $scope.changePassword = function() { |
| 42 | + $scope.pwd.submitted = true; |
| 43 | + if($scope.pwd.$valid) { |
10 | 44 | Auth.changePassword( $scope.user.oldPassword, $scope.user.newPassword )
|
11 | 45 | .then( function() {
|
12 |
| - $scope.message = 'Password successfully changed.'; |
| 46 | + $scope.message = 'Password successfully changed'; |
13 | 47 | })
|
14 | 48 | .catch( function() {
|
15 |
| - form.password.$setValidity('mongoose', false); |
| 49 | + $scope.pwd.old.$setValidity('mongoose', false); |
16 | 50 | $scope.errors.other = 'Incorrect password';
|
17 | 51 | $scope.message = '';
|
18 | 52 | });
|
19 | 53 | }
|
20 | 54 | };
|
| 55 | +<% if (filters.oauth) { %> |
| 56 | + $scope.setPassword = function() { |
| 57 | + $scope.submitted = true; |
| 58 | + if($scope.pwd.$valid) { |
| 59 | + Auth.changePassword( $scope.user.newPassword ) |
| 60 | + .then( function() { |
| 61 | + $scope.message = 'Password successfully set'; |
| 62 | + }) |
| 63 | + .catch( function() { |
| 64 | + $scope.pwd.old.$setValidity('mongoose', false); |
| 65 | + $scope.errors.other = 'Another account with that email already exists'; |
| 66 | + $scope.message = ''; |
| 67 | + }); |
| 68 | + } |
| 69 | + }; |
| 70 | +<% } %> |
21 | 71 | });
|
0 commit comments