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