Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit de08b55

Browse files
committed
[auth][docs] - Update auth documentation with onAuthChanged example for web api
1 parent 5e0ddd4 commit de08b55

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

docs/AUTHENTICATION.md

+30-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ const firebaseWebApi = require("nativescript-plugin-firebase/app");
6262
As stated [here](https://firebase.google.com/docs/auth/ios/manage-users#get_the_currently_signed-in_user):
6363

6464
> The recommended way to get the current user is by setting a listener on the Auth object
65-
65+
<details>
66+
<summary>Native API</summary>
6667
To listen to auth state changes you can register a listener during `init`:
6768

6869
```js
@@ -98,7 +99,35 @@ If - for some reason - you want more control over the listener you can use these
9899

99100
// check if already listening to auth state changes
100101
firebase.hasAuthStateListener(listener);
102+
```
103+
</details>
104+
105+
<details>
106+
<summary>Web API</summary>
107+
108+
The callback handler in will be called with the currentUser (undefined if not signed in) upon attaching the listener
109+
and when the auth state changes.
110+
111+
```js
112+
firebaseWebApi.auth().onAuthStateChanged((user?: User) => {
113+
console.log(">> auth state changed: " + user);
114+
if (user) {
115+
this.set("userEmailOrPhone", user.email ? user.email : (user.phoneNumber ? user.phoneNumber : "N/A"));
116+
alert({
117+
title: "User signed in",
118+
message: JSON.stringify(user),
119+
okButtonText: "Nice!"
120+
});
121+
} else {
122+
alert({
123+
title: "User signed out",
124+
okButtonText: "Bye!"
125+
});
126+
}
127+
},
128+
error => console.log("OnAuthChanged Error: " + error));
101129
```
130+
</details>
102131

103132
### Get Current User
104133
Once the user is logged in you can retrieve the currently logged in user:

0 commit comments

Comments
 (0)