Skip to content

Commit 90b7261

Browse files
authored
Update auth docs
1 parent eb3cd18 commit 90b7261

File tree

1 file changed

+198
-0
lines changed

1 file changed

+198
-0
lines changed

docs/api/authentication.md

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,199 @@
1+
# Authentication
12

3+
Firestack handles authentication for us out of the box, both with email/password-based authentication and through oauth providers (with a separate library to handle oauth providers).
4+
5+
> Android requires the Google Play services to installed for authentication to function.
6+
7+
## Local Auth
8+
9+
#### [listenForAuth()](https://firebase.google.com/docs/reference/js/firebase.auth.Auth#onAuthStateChanged)
10+
11+
Listen for changes in the users auth state (logging in and out).
12+
13+
```javascript
14+
firestack.auth.listenForAuth((evt) => {
15+
// evt is the authentication event, it contains an `error` key for carrying the
16+
// error message in case of an error and a `user` key upon successful authentication
17+
if (!evt.authenticated) {
18+
// There was an error or there is no user
19+
console.error(evt.error)
20+
} else {
21+
// evt.user contains the user details
22+
console.log('User details', evt.user);
23+
}
24+
})
25+
.then(() => console.log('Listening for authentication changes'))
26+
```
27+
28+
#### unlistenForAuth()
29+
30+
Remove the `listenForAuth` listener.
31+
This is important to release resources from our app when we don't need to hold on to the listener any longer.
32+
33+
```javascript
34+
firestack.auth.unlistenForAuth()
35+
```
36+
37+
#### [createUserWithEmail()](https://firebase.google.com/docs/reference/js/firebase.auth.Auth#createUserWithEmailAndPassword)
38+
39+
We can create a user by calling the `createUserWithEmail()` function.
40+
The method accepts two parameters, an email and a password.
41+
42+
```javascript
43+
firestack.auth.createUserWithEmail('[email protected]', '123456')
44+
.then((user) => {
45+
console.log('user created', user)
46+
})
47+
.catch((err) => {
48+
console.error('An error occurred', err);
49+
})
50+
```
51+
52+
#### [signInWithEmail()](https://firebase.google.com/docs/reference/js/firebase.auth.Auth#signInWithEmailAndPassword)
53+
54+
To sign a user in with their email and password, use the `signInWithEmail()` function.
55+
It accepts two parameters, the user's email and password:
56+
57+
```javascript
58+
firestack.auth.signInWithEmail('[email protected]', '123456')
59+
.then((user) => {
60+
console.log('User successfully logged in', user)
61+
})
62+
.catch((err) => {
63+
console.error('User signin error', err);
64+
})
65+
```
66+
67+
#### [signInAnonymously()](https://firebase.google.com/docs/reference/js/firebase.auth.Auth#signInAnonymously)
68+
69+
Sign an anonymous user. If the user has already signed in, that user will be returned.
70+
71+
```javascript
72+
firestack.auth.signInAnonymously()
73+
.then((user) => {
74+
console.log('Anonymous user successfully logged in', user)
75+
})
76+
.catch((err) => {
77+
console.error('Anonymous user signin error', err);
78+
})
79+
80+
#### signInWithProvider()
81+
82+
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.
83+
84+
> By using a separate library, we can keep our dependencies a little lower and the size of the application down.
85+
86+
#### signInWithCustomToken()
87+
88+
To sign a user using a self-signed custom token, use the `signInWithCustomToken()` function. It accepts one parameter, the custom token:
89+
90+
```javascript
91+
firestack.auth.signInWithCustomToken(TOKEN)
92+
.then((user) => {
93+
console.log('User successfully logged in', user)
94+
})
95+
.catch((err) => {
96+
console.error('User signin error', err);
97+
})
98+
```
99+
100+
#### [updateUserEmail()](https://firebase.google.com/docs/reference/js/firebase.User#updateEmail)
101+
102+
We can update the current user's email by using the command: `updateUserEmail()`.
103+
It accepts a single argument: the user's new email:
104+
105+
```javascript
106+
firestack.auth.updateUserEmail('[email protected]')
107+
.then((res) => console.log('Updated user email'))
108+
.catch(err => console.error('There was an error updating user email'))
109+
```
110+
111+
#### [updateUserPassword()](https://firebase.google.com/docs/reference/js/firebase.User#updatePassword)
112+
113+
We can update the current user's password using the `updateUserPassword()` method.
114+
It accepts a single parameter: the new password for the current user
115+
116+
```javascript
117+
firestack.auth.updateUserPassword('somethingReallyS3cr3t733t')
118+
.then(res => console.log('Updated user password'))
119+
.catch(err => console.error('There was an error updating your password'))
120+
```
121+
122+
#### [updateUserProfile()](https://firebase.google.com/docs/auth/web/manage-users#update_a_users_profile)
123+
124+
To update the current user's profile, we can call the `updateUserProfile()` method.
125+
It accepts a single parameter:
126+
127+
* object which contains updated key/values for the user's profile.
128+
Possible keys are listed [here](https://firebase.google.com/docs/auth/ios/manage-users#update_a_users_profile).
129+
130+
```javascript
131+
firestack.auth
132+
.updateUserProfile({
133+
displayName: 'Ari Lerner'
134+
})
135+
.then(res => console.log('Your profile has been updated'))
136+
.catch(err => console.error('There was an error :('))
137+
```
138+
139+
#### [sendPasswordResetWithEmail()](https://firebase.google.com/docs/auth/web/manage-users#send_a_password_reset_email)
140+
141+
To send a password reset for a user based upon their email, we can call the `sendPasswordResetWithEmail()` method.
142+
It accepts a single parameter: the email of the user to send a reset email.
143+
144+
```javascript
145+
firestack.auth.sendPasswordResetWithEmail('[email protected]')
146+
.then(res => console.log('Check your inbox for further instructions'))
147+
.catch(err => console.error('There was an error :('))
148+
```
149+
#### [deleteUser()](https://firebase.google.com/docs/auth/web/manage-users#delete_a_user)
150+
151+
It's possible to delete a user completely from your account on Firebase.
152+
Calling the `deleteUser()` method will take care of this for you.
153+
154+
```javascript
155+
firestack.auth
156+
.deleteUser()
157+
.then(res => console.log('Sad to see you go'))
158+
.catch(err => console.error('There was an error - Now you are trapped!'))
159+
```
160+
161+
#### getToken()
162+
163+
If you want user's token, use `getToken()` method.
164+
165+
```javascript
166+
firestack.auth
167+
.getToken()
168+
.then(res => console.log(res.token))
169+
.catch(err => console.error('error'))
170+
```
171+
172+
#### [signOut()](https://firebase.google.com/docs/reference/js/firebase.auth.Auth#signOut)
173+
174+
To sign the current user out, use the `signOut()` method.
175+
It accepts no parameters
176+
177+
```javascript
178+
firestack.auth
179+
.signOut()
180+
.then(res => console.log('You have been signed out'))
181+
.catch(err => console.error('Uh oh... something weird happened'))
182+
```
183+
184+
185+
#### getCurrentUser()
186+
187+
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()`.
188+
However, if you need to get the current user, call the `getCurrentUser()` method:
189+
190+
```javascript
191+
firestack.auth
192+
.getCurrentUser()
193+
.then(user => console.log('The currently logged in user', user))
194+
.catch(err => console.error('An error occurred'))
195+
```
196+
197+
## Social Auth
198+
199+
TODO

0 commit comments

Comments
 (0)