1
-
2
- import { NativeModules , NativeEventEmitter } from 'react-native' ;
1
+ import { NativeModules , NativeEventEmitter } from 'react-native' ;
3
2
const FirestackAuth = NativeModules . FirestackAuth
4
3
const FirestackAuthEvt = new NativeEventEmitter ( FirestackAuth ) ;
5
4
5
+
6
6
import promisify from '../utils/promisify'
7
7
import { Base } from './base'
8
+ import { default as User } from './user' ;
8
9
9
10
export class Authentication extends Base {
10
- constructor ( firestack , options = { } ) {
11
+ constructor ( firestack , options = { } ) {
11
12
super ( firestack , options ) ;
13
+ this . _authResult = null ;
14
+ this . authenticated = false ;
15
+ this . _user = null ;
16
+
17
+ // always track auth changes internall so we can access them synchronously
18
+ FirestackAuthEvt . addListener ( 'listenForAuth' , this . _onAuthStateChanged . bind ( this ) ) ;
19
+ FirestackAuth . listenForAuth ( ) ;
12
20
}
13
21
14
- // Auth
15
- listenForAuth ( callback ) {
16
- this . log . info ( 'Setting up listenForAuth callback' ) ;
17
- const sub = this . _on ( 'listenForAuth' , callback , FirestackAuthEvt ) ;
22
+ /**
23
+ * Internal auth changed listener
24
+ * @param auth
25
+ * @private
26
+ */
27
+ _onAuthStateChanged ( auth ) {
28
+ this . _authResult = auth ;
29
+ this . authenticated = auth ? auth . authenticated || false : false
30
+ if ( auth && ! this . _user ) this . _user = new User ( this , auth ) ;
31
+ else if ( ! auth && this . _user ) this . _user = null ;
32
+ else this . _user . _updateValues ( auth ) ;
33
+ }
34
+
35
+ /*
36
+ * WEB API
37
+ */
38
+
39
+ /**
40
+ * Listen for auth changes.
41
+ * @param callback
42
+ */
43
+ onAuthStateChanged ( listener ) {
44
+ this . log . info ( 'Creating onAuthStateChanged listener' ) ;
45
+ const sub = this . _on ( 'listenForAuth' , listener , FirestackAuthEvt ) ;
18
46
FirestackAuth . listenForAuth ( ) ;
19
- this . log . info ( 'Listening for auth ...' ) ;
47
+ this . log . info ( 'Listening for onAuthStateChanged events ...' ) ;
20
48
return promisify ( ( ) => sub , FirestackAuth ) ( sub ) ;
21
49
}
22
50
23
- unlistenForAuth ( ) {
24
- this . log . info ( 'Unlistening for auth' ) ;
51
+ /**
52
+ * Remove auth change listener
53
+ * @param listener
54
+ */
55
+ offAuthStateChanged ( listener ) {
56
+ this . log . info ( 'Removing onAuthStateChanged listener' ) ;
25
57
this . _off ( 'listenForAuth' ) ;
26
58
return promisify ( 'unlistenForAuth' , FirestackAuth ) ( ) ;
27
59
}
@@ -32,8 +64,8 @@ export class Authentication extends Base {
32
64
* @param {string } password The user's password
33
65
* @return {Promise } A promise indicating the completion
34
66
*/
35
- createUserWithEmail ( email , password ) {
36
- this . log . info ( 'Creating user with email' , email ) ;
67
+ createUserWithEmailAndPassword ( email , password ) {
68
+ this . log . info ( 'Creating user with email and password ' , email ) ;
37
69
return promisify ( 'createUserWithEmail' , FirestackAuth ) ( email , password ) ;
38
70
}
39
71
@@ -43,19 +75,44 @@ export class Authentication extends Base {
43
75
* @param {string } password The user's password
44
76
* @return {Promise } A promise that is resolved upon completion
45
77
*/
46
- signInWithEmail ( email , password ) {
78
+ signInWithEmailAndPassword ( email , password ) {
79
+ this . log . info ( 'Signing in user with email and password' , email ) ;
47
80
return promisify ( 'signInWithEmail' , FirestackAuth ) ( email , password )
48
81
}
49
82
83
+
50
84
/**
51
- * Sign the user in with a third-party authentication provider
52
- * @param {string } provider The name of the provider to use for login
53
- * @param {string } authToken The authToken granted by the provider
54
- * @param {string } authSecret The authToken secret granted by the provider
55
- * @return {Promise } A promise resolved upon completion
85
+ * Update the current user's email
86
+ * @param {string } email The user's _new_ email
87
+ * @return {Promise } A promise resolved upon completion
56
88
*/
57
- signInWithProvider ( provider , authToken , authSecret ) {
58
- return promisify ( 'signInWithProvider' , FirestackAuth ) ( provider , authToken , authSecret )
89
+ updateEmail ( email ) {
90
+ return promisify ( 'updateUserEmail' , FirestackAuth ) ( email ) ;
91
+ }
92
+
93
+ /**
94
+ * Send verification email to current user.
95
+ */
96
+ sendEmailVerification ( ) {
97
+ return promisify ( 'sendEmailVerification' , FirestackAuth ) ( ) ;
98
+ }
99
+
100
+ /**
101
+ * Update the current user's password
102
+ * @param {string } email the new password
103
+ * @return {Promise }
104
+ */
105
+ updatePassword ( password ) {
106
+ return promisify ( 'updateUserPassword' , FirestackAuth ) ( password ) ;
107
+ }
108
+
109
+ /**
110
+ * Update the current user's profile
111
+ * @param {Object } obj An object containing the keys listed [here](https://firebase.google.com/docs/auth/ios/manage-users#update_a_users_profile)
112
+ * @return {Promise }
113
+ */
114
+ updateProfile ( updates ) {
115
+ return promisify ( 'updateUserProfile' , FirestackAuth ) ( updates ) ;
59
116
}
60
117
61
118
/**
@@ -68,11 +125,14 @@ export class Authentication extends Base {
68
125
}
69
126
70
127
/**
71
- * Sign a user in anonymously
72
- * @return {Promise } A promise resolved upon completion
128
+ * Sign the user in with a third-party authentication provider
129
+ * @param {string } provider The name of the provider to use for login
130
+ * @param {string } authToken The authToken granted by the provider
131
+ * @param {string } authSecret The authToken secret granted by the provider
132
+ * @return {Promise } A promise resolved upon completion
73
133
*/
74
- signInAnonymously ( ) {
75
- return promisify ( 'signInAnonymously ' , FirestackAuth ) ( ) ;
134
+ signInWithProvider ( provider , authToken , authSecret ) {
135
+ return promisify ( 'signInWithProvider ' , FirestackAuth ) ( provider , authToken , authSecret )
76
136
}
77
137
78
138
/**
@@ -86,22 +146,72 @@ export class Authentication extends Base {
86
146
return promisify ( 'reauthenticateWithCredentialForProvider' , FirestackAuth ) ( provider , token , secret )
87
147
}
88
148
149
+
89
150
/**
90
- * Update the current user's email
91
- * @param {string } email The user's _new_ email
92
- * @return {Promise } A promise resolved upon completion
151
+ * Sign a user in anonymously
152
+ * @return {Promise } A promise resolved upon completion
93
153
*/
94
- updateUserEmail ( email ) {
95
- return promisify ( 'updateUserEmail ' , FirestackAuth ) ( email ) ;
154
+ signInAnonymously ( ) {
155
+ return promisify ( 'signInAnonymously ' , FirestackAuth ) ( ) ;
96
156
}
97
157
158
+
159
+ /*
160
+ * Old deprecated api stubs
161
+ */
162
+
163
+
98
164
/**
99
- * Update the current user's password
100
- * @param {string } email the new password
101
- * @return {Promise }
165
+ * @deprecated
166
+ * @param args
102
167
*/
103
- updatePassword ( password ) {
104
- return promisify ( 'updateUserPassword' , FirestackAuth ) ( password ) ;
168
+ listenForAuth ( ...args ) {
169
+ console . warn ( 'Firestack: listenForAuth is now deprecated, please use onAuthStateChanged' ) ;
170
+ this . onAuthStateChanged ( ...args ) ;
171
+ }
172
+
173
+ /**
174
+ * @deprecated
175
+ * @param args
176
+ */
177
+ unlistenForAuth ( ...args ) {
178
+ console . warn ( 'Firestack: unlistenForAuth is now deprecated, please use offAuthStateChanged' ) ;
179
+ this . offAuthStateChanged ( ...args ) ;
180
+ }
181
+
182
+ /**
183
+ * Create a user with the email/password functionality
184
+ * @deprecated
185
+ * @param {string } email The user's email
186
+ * @param {string } password The user's password
187
+ * @return {Promise } A promise indicating the completion
188
+ */
189
+ createUserWithEmail ( ...args ) {
190
+ console . warn ( 'Firestack: createUserWithEmail is now deprecated, please use createUserWithEmailAndPassword' ) ;
191
+ this . createUserWithEmailAndPassword ( ...args ) ;
192
+ }
193
+
194
+ /**
195
+ * Sign a user in with email/password
196
+ * @deprecated
197
+ * @param {string } email The user's email
198
+ * @param {string } password The user's password
199
+ * @return {Promise } A promise that is resolved upon completion
200
+ */
201
+ signInWithEmail ( ...args ) {
202
+ console . warn ( 'Firestack: signInWithEmail is now deprecated, please use signInWithEmailAndPassword' ) ;
203
+ this . signInWithEmailAndPassword ( ...args ) ;
204
+ }
205
+
206
+ /**
207
+ * Update the current user's email
208
+ * @deprecated
209
+ * @param {string } email The user's _new_ email
210
+ * @return {Promise } A promise resolved upon completion
211
+ */
212
+ updateUserEmail ( ...args ) {
213
+ console . warn ( 'Firestack: updateUserEmail is now deprecated, please use updateEmail' ) ;
214
+ this . updateEmail ( ...args ) ;
105
215
}
106
216
107
217
/**
@@ -119,6 +229,7 @@ export class Authentication extends Base {
119
229
deleteUser ( ) {
120
230
return promisify ( 'deleteUser' , FirestackAuth ) ( )
121
231
}
232
+
122
233
/**
123
234
* get the token of current user
124
235
* @return {Promise }
@@ -129,11 +240,13 @@ export class Authentication extends Base {
129
240
130
241
/**
131
242
* Update the current user's profile
243
+ * @deprecated
132
244
* @param {Object } obj An object containing the keys listed [here](https://firebase.google.com/docs/auth/ios/manage-users#update_a_users_profile)
133
245
* @return {Promise }
134
246
*/
135
- updateUserProfile ( obj ) {
136
- return promisify ( 'updateUserProfile' , FirestackAuth ) ( obj ) ;
247
+ updateUserProfile ( ...args ) {
248
+ console . warn ( 'Firestack: updateUserProfile is now deprecated, please use updateProfile' ) ;
249
+ this . updateProfile ( ...args ) ;
137
250
}
138
251
139
252
/**
@@ -148,8 +261,8 @@ export class Authentication extends Base {
148
261
* Get the currently signed in user
149
262
* @return {Promise }
150
263
*/
151
- getCurrentUser ( ) {
152
- return promisify ( 'getCurrentUser' , FirestackAuth ) ( ) ;
264
+ get currentUser ( ) {
265
+ return this . _user ;
153
266
}
154
267
155
268
get namespace ( ) {
0 commit comments