Skip to content

Commit 7e35007

Browse files
authored
Update authentication.md
1 parent 58507f0 commit 7e35007

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

docs/api/authentication.md

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

9-
#### [onAuthStateChanged(event: Function)](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).
9+
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.
1210

1311
```javascript
14-
firestack.auth().onAuthStateChanged((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-
```
12+
class Example extends React.Component {
2713

28-
#### offAuthStateChanged()
29-
30-
Remove the `onAuthStateChanged` listener.
31-
This is important to release resources from our app when we don't need to hold on to the listener any longer.
14+
constructor() {
15+
super();
16+
this.unsubscribe = null;
17+
}
18+
19+
componentDidMount() {
20+
this.unsubscribe = firebase.auth().onAuthStateChanged(function(user) {
21+
if (user) {
22+
// User is signed in.
23+
}
24+
});
25+
}
26+
27+
componentWillUnmount() {
28+
if (this.listener) {
29+
this.unsubscribe();
30+
}
31+
}
3232

33-
```javascript
34-
firestack.auth().offAuthStateChanged()
33+
}
3534
```
3635

36+
3737
#### [createUserWithEmailAndPassword(email: string, password: string)](https://firebase.google.com/docs/reference/js/firebase.auth.Auth#createUserWithEmailAndPassword)
3838

3939
We can create a user by calling the `createUserWithEmailAndPassword()` function.

0 commit comments

Comments
 (0)