1+ // @flow
12import { NativeModules , NativeEventEmitter } from 'react-native' ;
23
34import { Base } from './base' ;
@@ -7,26 +8,30 @@ import promisify from '../utils/promisify';
78const FirestackAuth = NativeModules . FirestackAuth ;
89const FirestackAuthEvt = new NativeEventEmitter ( FirestackAuth ) ;
910
11+ type AuthResultType = { authenticated : boolean , user : Object | null } ;
12+
1013export default class Auth extends Base {
11- constructor ( firestack , options = { } ) {
14+ _user : User | null ;
15+ _authResult : AuthResultType | null ;
16+ authenticated : boolean ;
17+
18+ constructor ( firestack : Object , options : Object = { } ) {
1219 super ( firestack , options ) ;
20+ this . _user = null ;
1321 this . _authResult = null ;
1422 this . authenticated = false ;
15- this . _user = null ;
1623
1724 // start listening straight away
1825 // generally though the initial event fired will get ignored
1926 // but this is ok as we fake it with the getCurrentUser below
2027 FirestackAuth . listenForAuth ( ) ;
2128
22- this . getCurrentUser ( ) . then ( ( u ) => {
23- const authResult = { authenticated : ! ! u } ;
24- if ( u ) authResult . user = u ;
25- this . _onAuthStateChanged ( authResult ) ;
29+ this . getCurrentUser ( ) . then ( ( u : Object ) => {
30+ this . _onAuthStateChanged ( { authenticated : ! ! u , user : u || null } ) ;
2631 this . _startListening ( ) ;
2732 } ) . catch ( ( ) => {
2833 // todo check if error contains user disabled message maybe and add a disabled flag?
29- this . _onAuthStateChanged ( { authenticated : false } ) ;
34+ this . _onAuthStateChanged ( { authenticated : false , user : null } ) ;
3035 this . _startListening ( ) ;
3136 } ) ;
3237 }
@@ -45,13 +50,13 @@ export default class Auth extends Base {
4550 * @param auth
4651 * @private
4752 */
48- _onAuthStateChanged ( auth ) {
53+ _onAuthStateChanged ( auth : AuthResultType ) {
4954 this . _authResult = auth ;
50- this . emit ( 'onAuthStateChanged' , this . _authResult . user || null ) ;
5155 this . authenticated = auth ? auth . authenticated || false : false ;
5256 if ( auth && auth . user && ! this . _user ) this . _user = new User ( this , auth ) ;
5357 else if ( ( ! auth || ! auth . user ) && this . _user ) this . _user = null ;
54- else this . _user ? this . _user . _updateValues ( auth ) : null ;
58+ else if ( this . _user ) this . _user . _updateValues ( auth ) ;
59+ this . emit ( 'onAuthStateChanged' , this . _authResult . user || null ) ;
5560 }
5661
5762 /*
@@ -62,7 +67,7 @@ export default class Auth extends Base {
6267 * Listen for auth changes.
6368 * @param listener
6469 */
65- onAuthStateChanged ( listener ) {
70+ onAuthStateChanged ( listener : Function ) {
6671 this . log . info ( 'Creating onAuthStateChanged listener' ) ;
6772 this . on ( 'onAuthStateChanged' , listener ) ;
6873 if ( this . _authResult ) listener ( this . _authResult . user || null ) ;
@@ -72,7 +77,7 @@ export default class Auth extends Base {
7277 * Remove auth change listener
7378 * @param listener
7479 */
75- offAuthStateChanged ( listener ) {
80+ offAuthStateChanged ( listener : Function ) {
7681 this . log . info ( 'Removing onAuthStateChanged listener' ) ;
7782 this . removeListener ( 'onAuthStateChanged' , listener ) ;
7883 }
@@ -83,7 +88,7 @@ export default class Auth extends Base {
8388 * @param {string } password The user's password
8489 * @return {Promise } A promise indicating the completion
8590 */
86- createUserWithEmailAndPassword ( email , password ) {
91+ createUserWithEmailAndPassword ( email : string , password : string ) : Promise < Object > {
8792 this . log . info ( 'Creating user with email and password' , email ) ;
8893 return promisify ( 'createUserWithEmail' , FirestackAuth ) ( email , password ) ;
8994 }
@@ -94,9 +99,9 @@ export default class Auth extends Base {
9499 * @param {string } password The user's password
95100 * @return {Promise } A promise that is resolved upon completion
96101 */
97- signInWithEmailAndPassword ( email , password ) {
102+ signInWithEmailAndPassword ( email : string , password : string ) : Promise < Object > {
98103 this . log . info ( 'Signing in user with email and password' , email ) ;
99- return promisify ( 'signInWithEmail' , FirestackAuth ) ( email , password )
104+ return promisify ( 'signInWithEmail' , FirestackAuth ) ( email , password ) ;
100105 }
101106
102107
@@ -105,14 +110,14 @@ export default class Auth extends Base {
105110 * @param {string } email The user's _new_ email
106111 * @return {Promise } A promise resolved upon completion
107112 */
108- updateEmail ( email ) {
113+ updateEmail ( email : string ) : Promise < Object > {
109114 return promisify ( 'updateUserEmail' , FirestackAuth ) ( email ) ;
110115 }
111116
112117 /**
113118 * Send verification email to current user.
114119 */
115- sendEmailVerification ( ) {
120+ sendEmailVerification ( ) : Promise < Object > {
116121 return promisify ( 'sendEmailVerification' , FirestackAuth ) ( ) ;
117122 }
118123
@@ -121,7 +126,7 @@ export default class Auth extends Base {
121126 * @param {string } password the new password
122127 * @return {Promise }
123128 */
124- updatePassword ( password ) {
129+ updatePassword ( password : string ) : Promise < Object > {
125130 return promisify ( 'updateUserPassword' , FirestackAuth ) ( password ) ;
126131 }
127132
@@ -130,7 +135,7 @@ export default class Auth extends Base {
130135 * @param {Object } updates An object containing the keys listed [here](https://firebase.google.com/docs/auth/ios/manage-users#update_a_users_profile)
131136 * @return {Promise }
132137 */
133- updateProfile ( updates ) {
138+ updateProfile ( updates : Object = { } ) : Promise < Object > {
134139 return promisify ( 'updateUserProfile' , FirestackAuth ) ( updates ) ;
135140 }
136141
@@ -139,62 +144,63 @@ export default class Auth extends Base {
139144 * @param {string } customToken A self-signed custom auth token.
140145 * @return {Promise } A promise resolved upon completion
141146 */
142- signInWithCustomToken ( customToken ) {
143- return promisify ( 'signInWithCustomToken' , FirestackAuth ) ( customToken )
147+ signInWithCustomToken ( customToken : string ) : Promise < Object > {
148+ return promisify ( 'signInWithCustomToken' , FirestackAuth ) ( customToken ) ;
144149 }
145150
151+ // TODO - below methods are not in web api / i think the're signInWithCredential
146152 /**
147153 * Sign the user in with a third-party authentication provider
148154 * @param {string } provider The name of the provider to use for login
149155 * @param {string } authToken The authToken granted by the provider
150156 * @param {string } authSecret The authToken secret granted by the provider
151157 * @return {Promise } A promise resolved upon completion
152158 */
153- signInWithProvider ( provider , authToken , authSecret ) {
154- return promisify ( 'signInWithProvider' , FirestackAuth ) ( provider , authToken , authSecret )
159+ signInWithProvider ( provider : string , authToken : string , authSecret : string ) : Promise < Object > {
160+ return promisify ( 'signInWithProvider' , FirestackAuth ) ( provider , authToken , authSecret ) ;
155161 }
156162
157163 /**
158- * Reauthenticate a user with a third-party authentication provider
164+ * Re-authenticate a user with a third-party authentication provider
159165 * @param {string } provider The provider name
160166 * @param {string } token The authToken granted by the provider
161167 * @param {string } secret The authTokenSecret granted by the provider
162168 * @return {Promise } A promise resolved upon completion
163169 */
164- reauthenticateWithCredentialForProvider ( provider , token , secret ) {
165- return promisify ( 'reauthenticateWithCredentialForProvider' , FirestackAuth ) ( provider , token , secret )
170+ reauthenticateWithCredentialForProvider ( provider : string , token : string , secret : string ) : Promise < Object > {
171+ return promisify ( 'reauthenticateWithCredentialForProvider' , FirestackAuth ) ( provider , token , secret ) ;
166172 }
167173
168174
169175 /**
170176 * Sign a user in anonymously
171177 * @return {Promise } A promise resolved upon completion
172178 */
173- signInAnonymously ( ) {
179+ signInAnonymously ( ) : Promise < Object > {
174180 return promisify ( 'signInAnonymously' , FirestackAuth ) ( ) ;
175181 }
176182
177183 /**
178184 * Send reset password instructions via email
179185 * @param {string } email The email to send password reset instructions
180186 */
181- sendPasswordResetWithEmail ( email ) {
187+ sendPasswordResetWithEmail ( email : string ) : Promise < Object > {
182188 return promisify ( 'sendPasswordResetWithEmail' , FirestackAuth ) ( email ) ;
183189 }
184190
185191 /**
186192 * Delete the current user
187193 * @return {Promise }
188194 */
189- deleteUser ( ) {
195+ deleteUser ( ) : Promise < Object > {
190196 return promisify ( 'deleteUser' , FirestackAuth ) ( ) ;
191197 }
192198
193199 /**
194200 * get the token of current user
195201 * @return {Promise }
196202 */
197- getToken ( ) {
203+ getToken ( ) : Promise < Object > {
198204 return promisify ( 'getToken' , FirestackAuth ) ( ) ;
199205 }
200206
@@ -203,27 +209,27 @@ export default class Auth extends Base {
203209 * Sign the current user out
204210 * @return {Promise }
205211 */
206- signOut ( ) {
212+ signOut ( ) : Promise < Object > {
207213 return promisify ( 'signOut' , FirestackAuth ) ( ) ;
208214 }
209215
210216 /**
211217 * Get the currently signed in user
212218 * @return {Promise }
213219 */
214- getCurrentUser ( ) {
220+ getCurrentUser ( ) : Promise < Object > {
215221 return promisify ( 'getCurrentUser' , FirestackAuth ) ( ) ;
216222 }
217223
218224 /**
219225 * Get the currently signed in user
220226 * @return {Promise }
221227 */
222- get currentUser ( ) {
228+ get currentUser ( ) : User | null {
223229 return this . _user ;
224230 }
225231
226- get namespace ( ) {
232+ get namespace ( ) : string {
227233 return 'firestack:auth' ;
228234 }
229235}
0 commit comments