@@ -4,6 +4,15 @@ 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 ` - Returns the current Firebase authentication state.
12
+ ##### ` currentUser: User | null ` - Returns the currently signed-in user (or null). See the [ User] ( /docs/api/authentication#user ) class documentation for further usage.
13
+
14
+ ### Methods
15
+
7
16
#### [ ` onAuthStateChanged(event: Function): Function ` ] ( https://firebase.google.com/docs/reference/js/firebase.auth.Auth#onAuthStateChanged )
8
17
9
18
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 +42,7 @@ class Example extends React.Component {
33
42
}
34
43
```
35
44
36
-
37
- #### [ createUserWithEmailAndPassword(email: string, password: string)] ( https://firebase.google.com/docs/reference/js/firebase.auth.Auth#createUserWithEmailAndPassword )
45
+ #### [ ` createUserWithEmailAndPassword(email: string, password: string): Promise ` ] ( https://firebase.google.com/docs/reference/js/firebase.auth.Auth#createUserWithEmailAndPassword )
38
46
39
47
We can create a user by calling the ` createUserWithEmailAndPassword() ` function.
40
48
The method accepts two parameters, an email and a password.
@@ -46,10 +54,10 @@ firestack.auth().createUserWithEmailAndPassword('ari@fullstack.io', '123456')
46
54
})
47
55
.catch ((err ) => {
48
56
console .error (' An error occurred' , err);
49
- })
57
+ });
50
58
```
51
59
52
- #### [ signInWithEmailAndPassword(email: string, password: string)] ( https://firebase.google.com/docs/reference/js/firebase.auth.Auth#signInWithEmailAndPassword )
60
+ #### [ ` signInWithEmailAndPassword(email: string, password: string): Promise ` ] ( https://firebase.google.com/docs/reference/js/firebase.auth.Auth#signInWithEmailAndPassword )
53
61
54
62
To sign a user in with their email and password, use the ` signInWithEmailAndPassword() ` function.
55
63
It accepts two parameters, the user's email and password:
@@ -61,10 +69,10 @@ firestack.auth().signInWithEmailAndPassword('ari@fullstack.io', '123456')
61
69
})
62
70
.catch ((err ) => {
63
71
console .error (' User signin error' , err);
64
- })
72
+ });
65
73
```
66
74
67
- #### [ signInAnonymously()] ( https://firebase.google.com/docs/reference/js/firebase.auth.Auth#signInAnonymously )
75
+ #### [ ` signInAnonymously(): Promise ` ] ( https://firebase.google.com/docs/reference/js/firebase.auth.Auth#signInAnonymously )
68
76
69
77
Sign an anonymous user. If the user has already signed in, that user will be returned.
70
78
@@ -75,126 +83,202 @@ firestack.auth().signInAnonymously()
75
83
})
76
84
.catch ((err ) => {
77
85
console .error (' Anonymous user signin error' , err);
78
- })
86
+ });
87
+ ```
88
+
89
+ #### [ ` signInWithCredential(credential: Object): Promise ` ] ( https://firebase.google.com/docs/reference/js/firebase.auth.Auth#signInWithCredential )
90
+
91
+ Sign in the user with a 3rd party credential provider. ` credential ` requires the following properties:
92
+
93
+ ``` javascript
94
+ {
95
+ provider: string,
96
+ token: string,
97
+ secret: string
98
+ }
79
99
```
80
100
81
- #### signInWithProvider()
101
+ ``` javascript
102
+ const credential = {
103
+ provider: ' facebook.com' ,
104
+ token: ' 12345' ,
105
+ secret: ' 6789' ,
106
+ };
82
107
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.
108
+ firestack .auth ().signInWithCredential (credential)
109
+ .then ((user ) => {
110
+ console .log (' User successfully signed in' , user)
111
+ })
112
+ .catch ((err ) => {
113
+ console .error (' User signin error' , err);
114
+ })
115
+ ```
84
116
85
- > By using a separate library, we can keep our dependencies a little lower and the size of the application down.
117
+ #### [ ` signInWithCustomToken(token: string): Promise ` ] ( https://firebase.google.com/docs/reference/js/firebase.auth.Auth#signInWithCustomToken )
86
118
87
- #### signInWithCustomToken()
119
+ Sign a user in with a self-signed [ JWT ] ( https://jwt.io ) token.
88
120
89
121
To sign a user using a self-signed custom token, use the ` signInWithCustomToken() ` function. It accepts one parameter, the custom token:
90
122
91
123
``` javascript
92
- firestack .auth ().signInWithCustomToken (TOKEN )
124
+ firestack .auth ().signInWithCustomToken (' 12345 ' )
93
125
.then ((user ) => {
94
126
console .log (' User successfully logged in' , user)
95
127
})
96
128
.catch ((err ) => {
97
129
console .error (' User signin error' , err);
98
- })
130
+ });
99
131
```
100
132
101
- #### [ updateUserEmail() ] ( https://firebase.google.com/docs/reference/js/firebase.User#updateEmail )
133
+ #### [ ` sendPasswordResetEmail(email: string): Promise ` ] ( https://firebase.google.com/docs/reference/js/firebase.auth.Auth#sendPasswordResetEmail )
102
134
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:
135
+ Sends a password reset email to the given email address. Unlike the web SDK, the email will contain a password reset link rather than a code.
105
136
106
137
``` javascript
107
- firestack .auth ().updateUserEmail (' ari+rocks@fullstack.io' )
108
- .then ((res ) => console .log (' Updated user email' ))
109
- .catch (err => console .error (' There was an error updating user email' ))
138
+ firestack .auth ().sendPasswordResetEmail (' foo@bar.com' )
139
+ .then (() => {
140
+ console .log (' Password reset email sent' );
141
+ })
142
+ .catch ((error ) => {
143
+ console .error (' Unable send password reset email' , error);
144
+ });
110
145
```
111
146
112
- #### [ updateUserPassword() ] ( https://firebase.google.com/docs/reference/js/firebase.User#updatePassword )
147
+ #### [ ` signOut(): Promise ` ] ( https://firebase.google.com/docs/reference/js/firebase.auth.Auth#confirmPasswordReset )
113
148
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
149
+ Completes the password reset process, given a confirmation code and new password.
116
150
117
151
``` 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' ))
152
+ firestack .auth ().signOut ()
153
+ .then (() => {
154
+ console .log (' User signed out successfully' );
155
+ })
156
+ .catch ();
121
157
```
122
158
123
- #### [ updateUserProfile()] ( https://firebase.google.com/docs/auth/web/manage-users#update_a_users_profile )
159
+ ## User
160
+
161
+ User class returned from ` firestack.auth().currentUser ` .
162
+
163
+ ### Properties
164
+
165
+ ##### ` displayName: string | null ` - The user's display name (if available).
166
+ ##### ` email: string | null ` - The user's email address (if available).
167
+ ##### ` emailVerified: boolean ` - True if the user's email address has been verified.
168
+ ##### ` isAnonymous: boolean `
169
+ ##### ` photoURL: string | null ` - The URL of the user's profile picture (if available).
170
+ ##### ` providerData: Object | null ` - Additional provider-specific information about the user.
171
+ ##### ` providerId: string | null ` - The authentication provider ID for the current user. For example, 'facebook.com', or 'google.com'.
172
+ ##### ` uid: string ` - The user's unique ID.
173
+
174
+ ### Methods
124
175
125
- To update the current user's profile, we can call the ` updateUserProfile() ` method.
126
- It accepts a single parameter:
176
+ #### [ ` delete(): Promise ` ] ( https://firebase.google.com/docs/reference/js/firebase.User#delete )
127
177
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 ) .
178
+ Delete the current user.
130
179
131
180
``` 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 :(' ))
181
+ firestack .auth ().currentUser
182
+ .delete ()
183
+ .then ()
184
+ .catch ();
138
185
```
139
186
140
- #### [ sendPasswordResetWithEmail() ] ( https://firebase.google.com/docs/auth/web/manage-users#send_a_password_reset_email )
187
+ #### [ ` getToken(): Promise ` ] ( https://firebase.google.com/docs/reference/js/firebase.User#getToken )
141
188
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.
189
+ Returns the users authentication token.
144
190
145
191
``` javascript
146
- firestack .auth ().sendPasswordResetWithEmail (' ari+rocks@fullstack.io' )
147
- .then (res => console .log (' Check your inbox for further instructions' ))
148
- .catch (err => console .error (' There was an error :(' ))
192
+ firestack .auth ().currentUser
193
+ .getToken ()
194
+ .then ((token ) => {})
195
+ .catch ();
149
196
```
150
- #### [ deleteUser()] ( https://firebase.google.com/docs/auth/web/manage-users#delete_a_user )
151
197
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.
198
+
199
+ #### [ ` reauthenticate(credential: Object): Promise ` ] ( https://firebase.google.com/docs/reference/js/firebase.User#reauthenticate )
200
+
201
+ Reauthenticate the current user with credentials:
154
202
155
203
``` 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!' ))
204
+ {
205
+ provider: string,
206
+ token: string,
207
+ secret: string
208
+ }
209
+ ```
210
+
211
+ ``` javascript
212
+ const credentials = {
213
+ provider: ' facebook.com' ,
214
+ token: ' 12345' ,
215
+ secret: ' 6789' ,
216
+ };
217
+
218
+ firestack .auth ().currentUser
219
+ .reauthenticate (credentials)
220
+ .then ()
221
+ .catch ();
160
222
```
161
223
162
- #### getToken( )
224
+ #### [ ` reload(): Promise ` ] ( https://firebase.google.com/docs/reference/js/firebase.User#reload )
163
225
164
- If you want user's token, use ` getToken() ` method .
226
+ Refreshes the current user.
165
227
166
228
``` javascript
167
- firestack .auth ()
229
+ firestack .auth (). currentUser
168
230
.getToken ()
169
- .then (res => console . log ( res . token ) )
170
- .catch (err => console . error ( ' error ' ))
231
+ .then (( user ) => {} )
232
+ .catch ();
171
233
```
172
234
173
- #### [ signOut() ] ( https://firebase.google.com/docs/reference/js/firebase.auth.Auth#signOut )
235
+ #### [ ` sendEmailVerification(): Promise ` ] ( https://firebase.google.com/docs/reference/js/firebase.User#sendEmailVerification )
174
236
175
- To sign the current user out, use the ` signOut() ` method.
176
- It accepts no parameters
237
+ Sends a verification email to a user. This will Promise reject is the user is anonymous.
177
238
178
239
``` 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 ' ))
240
+ firestack .auth (). currentUser
241
+ .sendEmailVerification ()
242
+ .then ()
243
+ .catch ();
183
244
```
184
245
246
+ #### [ updateEmail(email: string)] ( https://firebase.google.com/docs/reference/js/firebase.User#updateEmail )
247
+
248
+ 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.
185
249
186
- #### getCurrentUser()
250
+ ``` javascript
251
+ firestack .auth ().updateUserEmail (' foo@bar.com' )
252
+ .then ()
253
+ .catch ();
254
+ ```
187
255
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:
256
+ #### [ updatePassword(password: string)] ( https://firebase.google.com/docs/reference/js/firebase.User#updatePassword )
257
+
258
+ 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
259
191
260
``` 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' ))
261
+ firestack .auth ().updateUserPassword (' foobar1234' )
262
+ .then ()
263
+ .catch ();
196
264
```
197
265
198
- ## Social Auth
266
+ #### [ updateProfile(profile: Object)] ( https://firebase.google.com/docs/reference/js/firebase.User#updateProfile )
267
+
268
+ Updates a user's profile data. Profile data should be an object of fields to update:
269
+
270
+ ``` javascript
271
+ {
272
+ displayName: string,
273
+ photoURL: string,
274
+ }
275
+ ```
199
276
200
- TODO
277
+ ``` javascript
278
+ firestack .auth ()
279
+ .updateProfile ({
280
+ displayName: ' Ari Lerner'
281
+ })
282
+ .then ()
283
+ .catch ();
284
+ ```
0 commit comments