1-
2- import { NativeModules , NativeEventEmitter } from 'react-native' ;
1+ import { NativeModules , NativeEventEmitter } from 'react-native' ;
32const FirestackAuth = NativeModules . FirestackAuth
43const FirestackAuthEvt = new NativeEventEmitter ( FirestackAuth ) ;
54
5+
66import promisify from '../utils/promisify'
77import { Base } from './base'
8+ import { default as User } from './user' ;
89
910export class Authentication extends Base {
10- constructor ( firestack , options = { } ) {
11+ constructor ( firestack , options = { } ) {
1112 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 ( ) ;
1220 }
1321
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 ) ;
1846 FirestackAuth . listenForAuth ( ) ;
19- this . log . info ( 'Listening for auth ...' ) ;
47+ this . log . info ( 'Listening for onAuthStateChanged events ...' ) ;
2048 return promisify ( ( ) => sub , FirestackAuth ) ( sub ) ;
2149 }
2250
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' ) ;
2557 this . _off ( 'listenForAuth' ) ;
2658 return promisify ( 'unlistenForAuth' , FirestackAuth ) ( ) ;
2759 }
@@ -32,8 +64,8 @@ export class Authentication extends Base {
3264 * @param {string } password The user's password
3365 * @return {Promise } A promise indicating the completion
3466 */
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 ) ;
3769 return promisify ( 'createUserWithEmail' , FirestackAuth ) ( email , password ) ;
3870 }
3971
@@ -43,19 +75,44 @@ export class Authentication extends Base {
4375 * @param {string } password The user's password
4476 * @return {Promise } A promise that is resolved upon completion
4577 */
46- signInWithEmail ( email , password ) {
78+ signInWithEmailAndPassword ( email , password ) {
79+ this . log . info ( 'Signing in user with email and password' , email ) ;
4780 return promisify ( 'signInWithEmail' , FirestackAuth ) ( email , password )
4881 }
4982
83+
5084 /**
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
5688 */
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 ) ;
59116 }
60117
61118 /**
@@ -68,11 +125,14 @@ export class Authentication extends Base {
68125 }
69126
70127 /**
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
73133 */
74- signInAnonymously ( ) {
75- return promisify ( 'signInAnonymously ' , FirestackAuth ) ( ) ;
134+ signInWithProvider ( provider , authToken , authSecret ) {
135+ return promisify ( 'signInWithProvider ' , FirestackAuth ) ( provider , authToken , authSecret )
76136 }
77137
78138 /**
@@ -86,22 +146,72 @@ export class Authentication extends Base {
86146 return promisify ( 'reauthenticateWithCredentialForProvider' , FirestackAuth ) ( provider , token , secret )
87147 }
88148
149+
89150 /**
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
93153 */
94- updateUserEmail ( email ) {
95- return promisify ( 'updateUserEmail ' , FirestackAuth ) ( email ) ;
154+ signInAnonymously ( ) {
155+ return promisify ( 'signInAnonymously ' , FirestackAuth ) ( ) ;
96156 }
97157
158+
159+ /*
160+ * Old deprecated api stubs
161+ */
162+
163+
98164 /**
99- * Update the current user's password
100- * @param {string } email the new password
101- * @return {Promise }
165+ * @deprecated
166+ * @param args
102167 */
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 ) ;
105215 }
106216
107217 /**
@@ -119,6 +229,7 @@ export class Authentication extends Base {
119229 deleteUser ( ) {
120230 return promisify ( 'deleteUser' , FirestackAuth ) ( )
121231 }
232+
122233 /**
123234 * get the token of current user
124235 * @return {Promise }
@@ -129,11 +240,13 @@ export class Authentication extends Base {
129240
130241 /**
131242 * Update the current user's profile
243+ * @deprecated
132244 * @param {Object } obj An object containing the keys listed [here](https://firebase.google.com/docs/auth/ios/manage-users#update_a_users_profile)
133245 * @return {Promise }
134246 */
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 ) ;
137250 }
138251
139252 /**
@@ -148,8 +261,8 @@ export class Authentication extends Base {
148261 * Get the currently signed in user
149262 * @return {Promise }
150263 */
151- getCurrentUser ( ) {
152- return promisify ( 'getCurrentUser' , FirestackAuth ) ( ) ;
264+ get currentUser ( ) {
265+ return this . _user ;
153266 }
154267
155268 get namespace ( ) {
0 commit comments