@@ -4,6 +4,20 @@ Firestack handles authentication for us out of the box, both with email/password
4
4
5
5
> Android requires the Google Play services to installed for authentication to function.
6
6
7
+ ## Auth
8
+
9
+ ### Properties
10
+
11
+ ##### ` authenticated: boolean `
12
+
13
+ Returns the current Firebase authentication state.
14
+
15
+ ##### ` currentUser: [User](#) | null `
16
+
17
+ Returns the currently signed-in user (or null).
18
+
19
+ ### Methods
20
+
7
21
#### [ ` onAuthStateChanged(event: Function): Function ` ] ( https://firebase.google.com/docs/reference/js/firebase.auth.Auth#onAuthStateChanged )
8
22
9
23
Listen for changes in the users auth state (logging in and out). This method returns a unsubscribe function to stop listening to events. Always ensure you unsubscribe from the listener when no longer needed to prevent updates to components no longer in use.
@@ -33,8 +47,7 @@ class Example extends React.Component {
33
47
}
34
48
```
35
49
36
-
37
- #### [ createUserWithEmailAndPassword(email: string, password: string)] ( https://firebase.google.com/docs/reference/js/firebase.auth.Auth#createUserWithEmailAndPassword )
50
+ #### [ ` createUserWithEmailAndPassword(email: string, password: string): Promise ` ] ( https://firebase.google.com/docs/reference/js/firebase.auth.Auth#createUserWithEmailAndPassword )
38
51
39
52
We can create a user by calling the ` createUserWithEmailAndPassword() ` function.
40
53
The method accepts two parameters, an email and a password.
@@ -46,10 +59,10 @@ firestack.auth().createUserWithEmailAndPassword('
[email protected] ', '123456')
46
59
})
47
60
.catch ((err ) => {
48
61
console .error (' An error occurred' , err);
49
- })
62
+ });
50
63
```
51
64
52
- #### [ signInWithEmailAndPassword(email: string, password: string)] ( https://firebase.google.com/docs/reference/js/firebase.auth.Auth#signInWithEmailAndPassword )
65
+ #### [ ` signInWithEmailAndPassword(email: string, password: string): Promise ` ] ( https://firebase.google.com/docs/reference/js/firebase.auth.Auth#signInWithEmailAndPassword )
53
66
54
67
To sign a user in with their email and password, use the ` signInWithEmailAndPassword() ` function.
55
68
It accepts two parameters, the user's email and password:
@@ -61,10 +74,10 @@ firestack.auth().signInWithEmailAndPassword('
[email protected] ', '123456')
61
74
})
62
75
.catch ((err ) => {
63
76
console .error (' User signin error' , err);
64
- })
77
+ });
65
78
```
66
79
67
- #### [ signInAnonymously()] ( https://firebase.google.com/docs/reference/js/firebase.auth.Auth#signInAnonymously )
80
+ #### [ ` signInAnonymously(): Promise ` ] ( https://firebase.google.com/docs/reference/js/firebase.auth.Auth#signInAnonymously )
68
81
69
82
Sign an anonymous user. If the user has already signed in, that user will be returned.
70
83
@@ -75,126 +88,216 @@ firestack.auth().signInAnonymously()
75
88
})
76
89
.catch ((err ) => {
77
90
console .error (' Anonymous user signin error' , err);
78
- })
91
+ });
92
+ ```
93
+
94
+ #### [ ` signInWithCredential(credential: Object): Promise ` ] ( https://firebase.google.com/docs/reference/js/firebase.auth.Auth#signInWithCredential )
95
+
96
+ Sign in the user with a 3rd party credential provider. ` credential ` requires the following properties:
97
+
98
+ ``` javascript
99
+ {
100
+ provider: string,
101
+ token: string,
102
+ secret: string
103
+ }
79
104
```
80
105
81
- #### signInWithProvider()
106
+ ``` javascript
107
+ const credential = {
108
+ provider: ' facebook.com' ,
109
+ token: ' 12345' ,
110
+ secret: ' 6789' ,
111
+ };
82
112
83
- We can use an external authentication provider, such as twitter/facebook for authentication. In order to use an external provider, we need to include another library to handle authentication.
113
+ firestack .auth ().signInWithCredential (credential)
114
+ .then ((user ) => {
115
+ console .log (' User successfully signed in' , user)
116
+ })
117
+ .catch ((err ) => {
118
+ console .error (' User signin error' , err);
119
+ })
120
+ ```
84
121
85
- > By using a separate library, we can keep our dependencies a little lower and the size of the application down.
122
+ #### [ ` signInWithCustomToken(token: string): Promise ` ] ( https://firebase.google.com/docs/reference/js/firebase.auth.Auth#signInWithCustomToken )
86
123
87
- #### signInWithCustomToken()
124
+ Sign a user in with a self-signed [ JWT ] ( https://jwt.io ) token.
88
125
89
126
To sign a user using a self-signed custom token, use the ` signInWithCustomToken() ` function. It accepts one parameter, the custom token:
90
127
91
128
``` javascript
92
- firestack .auth ().signInWithCustomToken (TOKEN )
129
+ firestack .auth ().signInWithCustomToken (' 12345 ' )
93
130
.then ((user ) => {
94
131
console .log (' User successfully logged in' , user)
95
132
})
96
133
.catch ((err ) => {
97
134
console .error (' User signin error' , err);
135
+ });
136
+ ```
137
+
138
+ #### [ ` sendPasswordResetEmail(email: string): Promise ` ] ( https://firebase.google.com/docs/reference/js/firebase.auth.Auth#sendPasswordResetEmail )
139
+
140
+ Sends a password reset email to the given email address.
141
+
142
+ ``` javascript
143
+ firestack .
auth ().
sendPasswordResetEmail (
' [email protected] ' )
144
+ .then (() => {
145
+ console .log (' Password reset email sent' );
98
146
})
147
+ .catch ((error ) => {
148
+ console .error (' Unable send password reset email' , error);
149
+ });
99
150
```
100
151
101
- #### [ updateUserEmail() ] ( https://firebase.google.com/docs/reference/js/firebase.User#updateEmail )
152
+ #### [ ` confirmPasswordReset(code: string, newPassword: string): Promise ` ] ( https://firebase.google.com/docs/reference/js/firebase.auth.Auth#confirmPasswordReset )
102
153
103
- We can update the current user's email by using the command: ` updateUserEmail() ` .
104
- It accepts a single argument: the user's new email:
154
+ Completes the password reset process, given a confirmation code and new password.
105
155
106
156
``` javascript
107
- firestack .
auth ().
updateUserEmail (
' [email protected] ' )
108
- .then ((res ) => console .log (' Updated user email' ))
109
- .catch (err => console .error (' There was an error updating user email' ))
157
+ firestack .auth ().confirmPasswordReset (' 1234' , ' barfoo4321' )
158
+ .then (() => {
159
+ console .log (' Password reset successfully' );
160
+ })
161
+ .catch ((error ) => {
162
+ console .error (' Unable to reset password' , error);
163
+ });
110
164
```
111
165
112
- #### [ updateUserPassword() ] ( https://firebase.google.com/docs/reference/js/firebase.User#updatePassword )
166
+ #### [ ` signOut(): Promise ` ] ( https://firebase.google.com/docs/reference/js/firebase.auth.Auth#confirmPasswordReset )
113
167
114
- We can update the current user's password using the ` updateUserPassword() ` method.
115
- It accepts a single parameter: the new password for the current user
168
+ Completes the password reset process, given a confirmation code and new password.
116
169
117
170
``` javascript
118
- firestack .auth ().updateUserPassword (' somethingReallyS3cr3t733t' )
119
- .then (res => console .log (' Updated user password' ))
120
- .catch (err => console .error (' There was an error updating your password' ))
171
+ firestack .auth ().signOut ()
172
+ .then (() => {
173
+ console .log (' User signed out successfully' );
174
+ })
175
+ .catch ();
121
176
```
122
177
123
- #### [ updateUserProfile()] ( https://firebase.google.com/docs/auth/web/manage-users#update_a_users_profile )
178
+ ## User
179
+
180
+ User class returned from ` firestack.auth().currentUser ` .
124
181
125
- To update the current user's profile, we can call the ` updateUserProfile() ` method.
126
- It accepts a single parameter:
182
+ ### Properties
127
183
128
- * object which contains updated key/values for the user's profile.
129
- Possible keys are listed [ here] ( https://firebase.google.com/docs/auth/ios/manage-users#update_a_users_profile ) .
184
+ ##### ` displayName: string | null ` - The user's display name (if available).
185
+ ##### ` email: string | null ` - The user's email address (if available).
186
+ ##### ` emailVerified: boolean ` - True if the user's email address has been verified.
187
+ ##### ` isAnonymous: boolean `
188
+ ##### ` photoURL: string | null ` - The URL of the user's profile picture (if available).
189
+ ##### ` providerData: Object | null ` - Additional provider-specific information about the user.
190
+ ##### ` providerId: string | null ` - The authentication provider ID for the current user. For example, 'facebook.com', or 'google.com'.
191
+ ##### ` uid: string ` - The user's unique ID.
192
+
193
+ ### Methods
194
+
195
+ #### [ ` delete(): Promise ` ] ( https://firebase.google.com/docs/reference/js/firebase.User#delete )
196
+
197
+ Delete the current user.
130
198
131
199
``` javascript
132
- firestack .auth ()
133
- .updateUserProfile ({
134
- displayName: ' Ari Lerner'
135
- })
136
- .then (res => console .log (' Your profile has been updated' ))
137
- .catch (err => console .error (' There was an error :(' ))
200
+ firestack .auth ().currentUser
201
+ .delete ()
202
+ .then ()
203
+ .catch ();
138
204
```
139
205
140
- #### [ sendPasswordResetWithEmail() ] ( https://firebase.google.com/docs/auth/web/manage-users#send_a_password_reset_email )
206
+ #### [ ` getToken(): Promise ` ] ( https://firebase.google.com/docs/reference/js/firebase.User#getToken )
141
207
142
- To send a password reset for a user based upon their email, we can call the ` sendPasswordResetWithEmail() ` method.
143
- It accepts a single parameter: the email of the user to send a reset email.
208
+ Returns the users authentication token.
144
209
145
210
``` javascript
146
- firestack .
auth ().
sendPasswordResetWithEmail (
' [email protected] ' )
147
- .then (res => console .log (' Check your inbox for further instructions' ))
148
- .catch (err => console .error (' There was an error :(' ))
211
+ firestack .auth ().currentUser
212
+ .getToken ()
213
+ .then ((token ) => {})
214
+ .catch ();
149
215
```
150
- #### [ deleteUser()] ( https://firebase.google.com/docs/auth/web/manage-users#delete_a_user )
151
216
152
- It's possible to delete a user completely from your account on Firebase.
153
- Calling the ` deleteUser() ` method will take care of this for you.
217
+
218
+ #### [ ` reauthenticate(credential: Object): Promise ` ] ( https://firebase.google.com/docs/reference/js/firebase.User#reauthenticate )
219
+
220
+ Reauthenticate the current user with credentials:
154
221
155
222
``` javascript
156
- firestack .auth ()
157
- .deleteUser ()
158
- .then (res => console .log (' Sad to see you go' ))
159
- .catch (err => console .error (' There was an error - Now you are trapped!' ))
223
+ {
224
+ provider: string,
225
+ token: string,
226
+ secret: string
227
+ }
228
+ ```
229
+
230
+ ``` javascript
231
+ const credentials = {
232
+ provider: ' facebook.com' ,
233
+ token: ' 12345' ,
234
+ secret: ' 6789' ,
235
+ };
236
+
237
+ firestack .auth ().currentUser
238
+ .reauthenticate (credentials)
239
+ .then ()
240
+ .catch ();
160
241
```
161
242
162
- #### getToken( )
243
+ #### [ ` reload(): Promise ` ] ( https://firebase.google.com/docs/reference/js/firebase.User#reload )
163
244
164
- If you want user's token, use ` getToken() ` method .
245
+ Refreshes the current user.
165
246
166
247
``` javascript
167
- firestack .auth ()
248
+ firestack .auth (). currentUser
168
249
.getToken ()
169
- .then (res => console . log ( res . token ) )
170
- .catch (err => console . error ( ' error ' ))
250
+ .then (( user ) => {} )
251
+ .catch ();
171
252
```
172
253
173
- #### [ signOut() ] ( https://firebase.google.com/docs/reference/js/firebase.auth.Auth#signOut )
254
+ #### [ ` sendEmailVerification(): Promise ` ] ( https://firebase.google.com/docs/reference/js/firebase.User#sendEmailVerification )
174
255
175
- To sign the current user out, use the ` signOut() ` method.
176
- It accepts no parameters
256
+ Sends a verification email to a user. This will Promise reject is the user is anonymous.
177
257
178
258
``` javascript
179
- firestack .auth ()
180
- .signOut ()
181
- .then (res => console . log ( ' You have been signed out ' ) )
182
- .catch (err => console . error ( ' Uh oh... something weird happened ' ))
259
+ firestack .auth (). currentUser
260
+ .sendEmailVerification ()
261
+ .then ()
262
+ .catch ();
183
263
```
184
264
265
+ #### [ updateEmail(email: string)] ( https://firebase.google.com/docs/reference/js/firebase.User#updateEmail )
185
266
186
- #### getCurrentUser()
267
+ Updates the user's email address. See Firebase docs for more information on security & email validation. This will Promise reject is the user is anonymous.
187
268
188
- Although you _ can_ get the current user using the ` getCurrentUser() ` method, it's better to use this from within the callback function provided by ` listenForAuth() ` .
189
- However, if you need to get the current user, call the ` getCurrentUser() ` method:
269
+ ``` javascript
270
+ firestack .
auth ().
updateUserEmail (
' [email protected] ' )
271
+ .then ()
272
+ .catch ();
273
+ ```
274
+
275
+ #### [ updatePassword(password: string)] ( https://firebase.google.com/docs/reference/js/firebase.User#updatePassword )
276
+
277
+ Important: this is a security sensitive operation that requires the user to have recently signed in. If this requirement isn't met, ask the user to authenticate again and then call firebase.User#reauthenticate. This will Promise reject is the user is anonymous.
190
278
191
279
``` javascript
192
- firestack .auth ()
193
- .getCurrentUser ()
194
- .then (user => console .log (' The currently logged in user' , user))
195
- .catch (err => console .error (' An error occurred' ))
280
+ firestack .auth ().updateUserPassword (' foobar1234' )
281
+ .then ()
282
+ .catch ();
196
283
```
197
284
198
- ## Social Auth
285
+ #### [ updateProfile(profile: Object)] ( https://firebase.google.com/docs/reference/js/firebase.User#updateProfile )
286
+
287
+ Updates a user's profile data. Profile data should be an object of fields to update:
199
288
200
- TODO
289
+ ``` javascript
290
+ {
291
+ displayName: string,
292
+ photoURL: string,
293
+ }
294
+ ```
295
+
296
+ ``` javascript
297
+ firestack .auth ()
298
+ .updateProfile ({
299
+ displayName: ' Ari Lerner'
300
+ })
301
+ .then ()
302
+ .catch ();
303
+ ```
0 commit comments