1
- ( function ( angular ) {
2
- angular . module ( 'firebase.auth' , [ 'firebase' , 'firebase.utils' ] )
3
-
4
- // a simple wrapper on simpleLogin.getUser() that rejects the promise
5
- // if the user does not exists (i.e. makes user required)
6
- . factory ( 'requireUser' , [ 'Auth' , '$q' , function ( Auth , $q ) {
7
- return function ( ) {
8
- return Auth . getUser ( ) . then ( function ( user ) {
9
- return user ? user : $q . reject ( { authRequired : true } ) ;
10
- } ) ;
11
- }
12
- } ] )
13
-
14
- . factory ( 'Auth' , [ '$firebaseAuth' , 'fbutil' , function ( $firebaseAuth , fbutil ) {
15
- var auth = $firebaseAuth ( fbutil . ref ( ) ) ;
16
- var listeners = [ ] ;
17
-
18
- function statusChange ( ) {
19
- fns . user = auth . $getAuth ( ) ;
20
- angular . forEach ( listeners , function ( fn ) {
21
- fn ( fns . user ) ;
22
- } ) ;
23
- }
24
-
25
- var fns = {
26
- user : null ,
27
-
28
- getUser : function ( ) {
29
- return auth . $waitForAuth ( ) ;
30
- } ,
31
-
32
- /**
33
- * @param {string } email
34
- * @param {string } pass
35
- * @returns {* }
36
- */
37
- login : function ( email , pass ) {
38
- return auth . $authWithPassword ( {
39
- email : email ,
40
- password : pass
41
- } , { rememberMe : true } ) ;
42
- } ,
43
-
44
- logout : function ( ) {
45
- auth . $unauth ( ) ;
46
- } ,
47
-
48
- createAccount : function ( email , pass , name ) {
49
- return auth . $createUser ( { email : email , password : pass } )
50
- . then ( function ( ) {
51
- // authenticate so we have permission to write to Firebase
52
- return fns . login ( email , pass ) ;
53
- } )
54
- . then ( function ( user ) {
55
- var ref = fbutil . ref ( 'users' , user . uid ) ;
56
- return fbutil . handler ( function ( cb ) {
57
- ref . set ( { email : email , name : name || firstPartOfEmail ( email ) } , cb ) ;
58
- } ) ;
59
- } ) ;
60
- } ,
61
-
62
- changePassword : function ( email , oldpass , newpass ) {
63
- return auth . $changePassword ( { email : email , oldPassword : oldpass , newPassword : newpass } ) ;
64
- } ,
65
-
66
- changeEmail : function ( password , oldEmail , newEmail ) {
67
- return auth . $changeEmail ( { password : password , oldEmail : oldEmail , newEmail : newEmail } , this )
68
- . then ( function ( ) {
69
- return fbutil . handler ( function ( cb ) {
70
- var ref = fbutil . ref ( 'users' , fns . user . uid , 'email' ) ;
71
- ref . set ( newEmail , cb ) ;
72
- } ) ;
73
- } ) ;
74
- } ,
75
-
76
- removeUser : function ( email , pass ) {
77
- return auth . $removeUser ( { email : email , password : pass } ) ;
78
- } ,
79
-
80
- watch : function ( cb , $scope ) {
81
- fns . getUser ( ) . then ( function ( user ) {
82
- cb ( user ) ;
83
- } ) ;
84
- listeners . push ( cb ) ;
85
- var unbind = function ( ) {
86
- var i = listeners . indexOf ( cb ) ;
87
- if ( i > - 1 ) { listeners . splice ( i , 1 ) ; }
88
- } ;
89
- if ( $scope ) {
90
- $scope . $on ( '$destroy' , unbind ) ;
91
- }
92
- return unbind ;
93
- }
94
- } ;
95
-
96
- auth . $onAuth ( statusChange ) ;
97
- statusChange ( ) ;
98
-
99
- return fns ;
100
- } ] ) ;
101
-
102
-
103
- function firstPartOfEmail ( email ) {
104
- return ucfirst ( email . substr ( 0 , email . indexOf ( '@' ) ) || '' ) ;
105
- }
106
-
107
- function ucfirst ( str ) {
108
- // inspired by: http://kevin.vanzonneveld.net
109
- str += '' ;
110
- var f = str . charAt ( 0 ) . toUpperCase ( ) ;
111
- return f + str . substr ( 1 ) ;
112
- }
113
- } ) ( angular ) ;
1
+ angular . module ( 'firebase.auth' , [ 'firebase' , 'firebase.utils' ] )
2
+ . factory ( 'Auth' , [ '$firebaseAuth' , 'fbutil' , function ( $firebaseAuth , fbutil ) {
3
+ return $firebaseAuth ( fbutil . ref ( ) ) ;
4
+ } ] ) ;
5
+
6
+ // var fns = {
7
+ // user: null,
8
+ //
9
+ // getUser: function() {
10
+ // return auth.$waitForAuth();
11
+ // },
12
+ //
13
+ // /**
14
+ // * @param {string } email
15
+ // * @param {string } pass
16
+ // * @returns {* }
17
+ // */
18
+ // login: function(email, pass) {
19
+ // return auth.$authWithPassword({
20
+ // email: email,
21
+ // password: pass
22
+ // }, {rememberMe: true});
23
+ // },
24
+ //
25
+ // logout: function() {
26
+ // auth.$unauth();
27
+ // },
28
+ //
29
+ // createAccount: function(email, pass, name) {
30
+ // return auth.$createUser({email: email, password: pass})
31
+ // .then(function() {
32
+ // // authenticate so we have permission to write to Firebase
33
+ // return fns.login(email, pass);
34
+ // })
35
+ // .then(function(user) {
36
+ // var ref = fbutil.ref('users', user.uid);
37
+ // return fbutil.handler(function(cb) {
38
+ // ref.set({email: email, name: name||firstPartOfEmail(email)}, cb);
39
+ // });
40
+ // });
41
+ // },
42
+ //
43
+ // changePassword: function(email, oldpass, newpass) {
44
+ // return auth.$changePassword({email: email, oldPassword: oldpass, newPassword: newpass});
45
+ // },
46
+ //
47
+ // changeEmail: function(password, oldEmail, newEmail) {
48
+ // return auth.$changeEmail({password: password, oldEmail: oldEmail, newEmail: newEmail}, this)
49
+ // .then(function() {
50
+ // return fbutil.handler(function(cb) {
51
+ // var ref = fbutil.ref('users', fns.user.uid, 'email');
52
+ // ref.set(newEmail, cb);
53
+ // });
54
+ // });
55
+ // },
56
+ //
57
+ // removeUser: function(email, pass) {
58
+ // return auth.$removeUser({email: email, password: pass});
59
+ // },
60
+ //
61
+ // watch: function(cb, $scope) {
62
+ // fns.getUser().then(function(user) {
63
+ // cb(user);
64
+ // });
65
+ // listeners.push(cb);
66
+ // var unbind = function() {
67
+ // var i = listeners.indexOf(cb);
68
+ // if( i > -1 ) { listeners.splice(i, 1); }
69
+ // };
70
+ // if( $scope ) {
71
+ // $scope.$on('$destroy', unbind);
72
+ // }
73
+ // return unbind;
74
+ // }
75
+ // };
76
+ //
77
+ // auth.$onAuth(statusChange);
78
+ // statusChange();
79
+ //
80
+ // return fns;
81
+ // }]);
82
+ //
83
+ //
84
+ //function firstPartOfEmail(email) {
85
+ // return ucfirst(email.substr(0, email.indexOf('@'))||'');
86
+ //}
87
+ //
88
+ //function ucfirst (str) {
89
+ // // inspired by: http://kevin.vanzonneveld.net
90
+ // str += '';
91
+ // var f = str.charAt(0).toUpperCase();
92
+ // return f + str.substr(1);
93
+ //}
0 commit comments