This repository has been archived by the owner on Aug 25, 2018. It is now read-only.
/
account.js
77 lines (68 loc) · 2.52 KB
/
account.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
(function (angular) {
"use strict";
var app = angular.module('myApp.account', ['firebase', 'firebase.utils', 'firebase.auth', 'ngRoute']);
app.controller('AccountCtrl', ['$scope', 'Auth', 'fbutil', 'user', '$location', '$firebaseObject',
function($scope, Auth, fbutil, user, $location, $firebaseObject) {
var unbind;
// create a 3-way binding with the user profile object in Firebase
var profile = $firebaseObject(fbutil.ref('users', user.uid));
profile.$bindTo($scope, 'profile').then(function(ub) { unbind = ub; });
// expose logout function to scope
$scope.logout = function() {
if( unbind ) { unbind(); }
profile.$destroy();
Auth.$unauth();
$location.path('/login');
};
$scope.changePassword = function(pass, confirm, newPass) {
resetMessages();
if( !pass || !confirm || !newPass ) {
$scope.err = 'Please fill in all password fields';
}
else if( newPass !== confirm ) {
$scope.err = 'New pass and confirm do not match';
}
else {
Auth.$changePassword({email: profile.email, oldPassword: pass, newPassword: newPass})
.then(function() {
$scope.msg = 'Password changed';
}, function(err) {
$scope.err = err;
})
}
};
$scope.clear = resetMessages;
$scope.changeEmail = function(pass, newEmail) {
resetMessages();
var oldEmail = profile.email;
Auth.$changeEmail({oldEmail: oldEmail, newEmail: newEmail, password: pass})
.then(function() {
// store the new email address in the user's profile
return fbutil.handler(function(done) {
fbutil.ref('users', user.uid, 'email').set(newEmail, done);
});
})
.then(function() {
$scope.emailmsg = 'Email changed';
}, function(err) {
$scope.emailerr = err;
});
};
function resetMessages() {
$scope.err = null;
$scope.msg = null;
$scope.emailerr = null;
$scope.emailmsg = null;
}
}
]);
app.config(['$routeProvider', function($routeProvider) {
// require user to be authenticated before they can access this page
// this is handled by the .whenAuthenticated method declared in
// components/router/router.js
$routeProvider.whenAuthenticated('/account', {
templateUrl: 'account/account.html',
controller: 'AccountCtrl'
})
}]);
})(angular);