Skip to content

Commit af51355

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents d237eb9 + 5cfd01f commit af51355

File tree

2 files changed

+318
-228
lines changed

2 files changed

+318
-228
lines changed

docs/api/authentication.md

Lines changed: 154 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ 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` - 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+
716
#### [`onAuthStateChanged(event: Function): Function`](https://firebase.google.com/docs/reference/js/firebase.auth.Auth#onAuthStateChanged)
817

918
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 {
3342
}
3443
```
3544

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

3947
We can create a user by calling the `createUserWithEmailAndPassword()` function.
4048
The method accepts two parameters, an email and a password.
@@ -46,10 +54,10 @@ firestack.auth().createUserWithEmailAndPassword('ari@fullstack.io', '123456')
4654
})
4755
.catch((err) => {
4856
console.error('An error occurred', err);
49-
})
57+
});
5058
```
5159

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

5462
To sign a user in with their email and password, use the `signInWithEmailAndPassword()` function.
5563
It accepts two parameters, the user's email and password:
@@ -61,10 +69,10 @@ firestack.auth().signInWithEmailAndPassword('ari@fullstack.io', '123456')
6169
})
6270
.catch((err) => {
6371
console.error('User signin error', err);
64-
})
72+
});
6573
```
6674

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

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

@@ -75,126 +83,202 @@ firestack.auth().signInAnonymously()
7583
})
7684
.catch((err) => {
7785
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+
}
7999
```
80100

81-
#### signInWithProvider()
101+
```javascript
102+
const credential = {
103+
provider: 'facebook.com',
104+
token: '12345',
105+
secret: '6789',
106+
};
82107

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

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

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

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

91123
```javascript
92-
firestack.auth().signInWithCustomToken(TOKEN)
124+
firestack.auth().signInWithCustomToken('12345')
93125
.then((user) => {
94126
console.log('User successfully logged in', user)
95127
})
96128
.catch((err) => {
97129
console.error('User signin error', err);
98-
})
130+
});
99131
```
100132

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

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

106137
```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+
});
110145
```
111146

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

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

117151
```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();
121157
```
122158

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
124175

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

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

131180
```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();
138185
```
139186

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

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

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

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

155203
```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();
160222
```
161223

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

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

166228
```javascript
167-
firestack.auth()
229+
firestack.auth().currentUser
168230
.getToken()
169-
.then(res => console.log(res.token))
170-
.catch(err => console.error('error'))
231+
.then((user) => {})
232+
.catch();
171233
```
172234

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

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

178239
```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();
183244
```
184245

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

186-
#### getCurrentUser()
250+
```javascript
251+
firestack.auth().updateUserEmail('foo@bar.com')
252+
.then()
253+
.catch();
254+
```
187255

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

191260
```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();
196264
```
197265

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

200-
TODO
277+
```javascript
278+
firestack.auth()
279+
.updateProfile({
280+
displayName: 'Ari Lerner'
281+
})
282+
.then()
283+
.catch();
284+
```

0 commit comments

Comments
 (0)