Skip to content

Commit 77d36d3

Browse files
authored
Update authentication.md
1 parent cd40d73 commit 77d36d3

File tree

1 file changed

+172
-69
lines changed

1 file changed

+172
-69
lines changed

docs/api/authentication.md

Lines changed: 172 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@ Firestack handles authentication for us out of the box, both with email/password
44

55
> Android requires the Google Play services to installed for authentication to function.
66
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+
721
#### [`onAuthStateChanged(event: Function): Function`](https://firebase.google.com/docs/reference/js/firebase.auth.Auth#onAuthStateChanged)
822

923
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 {
3347
}
3448
```
3549

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)
3851

3952
We can create a user by calling the `createUserWithEmailAndPassword()` function.
4053
The method accepts two parameters, an email and a password.
@@ -46,10 +59,10 @@ firestack.auth().createUserWithEmailAndPassword('[email protected]', '123456')
4659
})
4760
.catch((err) => {
4861
console.error('An error occurred', err);
49-
})
62+
});
5063
```
5164

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)
5366

5467
To sign a user in with their email and password, use the `signInWithEmailAndPassword()` function.
5568
It accepts two parameters, the user's email and password:
@@ -61,10 +74,10 @@ firestack.auth().signInWithEmailAndPassword('[email protected]', '123456')
6174
})
6275
.catch((err) => {
6376
console.error('User signin error', err);
64-
})
77+
});
6578
```
6679

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)
6881

6982
Sign an anonymous user. If the user has already signed in, that user will be returned.
7083

@@ -75,126 +88,216 @@ firestack.auth().signInAnonymously()
7588
})
7689
.catch((err) => {
7790
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+
}
79104
```
80105

81-
#### signInWithProvider()
106+
```javascript
107+
const credential = {
108+
provider: 'facebook.com',
109+
token: '12345',
110+
secret: '6789',
111+
};
82112

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+
```
84121

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)
86123

87-
#### signInWithCustomToken()
124+
Sign a user in with a self-signed [JWT](https://jwt.io) token.
88125

89126
To sign a user using a self-signed custom token, use the `signInWithCustomToken()` function. It accepts one parameter, the custom token:
90127

91128
```javascript
92-
firestack.auth().signInWithCustomToken(TOKEN)
129+
firestack.auth().signInWithCustomToken('12345')
93130
.then((user) => {
94131
console.log('User successfully logged in', user)
95132
})
96133
.catch((err) => {
97134
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');
98146
})
147+
.catch((error) => {
148+
console.error('Unable send password reset email', error);
149+
});
99150
```
100151

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)
102153

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.
105155

106156
```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+
});
110164
```
111165

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)
113167

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.
116169

117170
```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();
121176
```
122177

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`.
124181

125-
To update the current user's profile, we can call the `updateUserProfile()` method.
126-
It accepts a single parameter:
182+
### Properties
127183

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.
130198

131199
```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();
138204
```
139205

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)
141207

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.
144209

145210
```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();
149215
```
150-
#### [deleteUser()](https://firebase.google.com/docs/auth/web/manage-users#delete_a_user)
151216

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:
154221

155222
```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();
160241
```
161242

162-
#### getToken()
243+
#### [`reload(): Promise`](https://firebase.google.com/docs/reference/js/firebase.User#reload)
163244

164-
If you want user's token, use `getToken()` method.
245+
Refreshes the current user.
165246

166247
```javascript
167-
firestack.auth()
248+
firestack.auth().currentUser
168249
.getToken()
169-
.then(res => console.log(res.token))
170-
.catch(err => console.error('error'))
250+
.then((user) => {})
251+
.catch();
171252
```
172253

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)
174255

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.
177257

178258
```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();
183263
```
184264

265+
#### [updateEmail(email: string)](https://firebase.google.com/docs/reference/js/firebase.User#updateEmail)
185266

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.
187268

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.
190278

191279
```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();
196283
```
197284

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:
199288

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

Comments
 (0)