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

Commit ed8b72c

Browse files
Merge remote-tracking branch 'origin/master'
2 parents 71439ec + de08b55 commit ed8b72c

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
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:

src/app/auth/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,12 @@ export namespace auth {
4444
}
4545

4646
// Completed will never be called, but it is here to closely follow the web api interface.
47+
// When called, the callback handler will be passed in the currentUser or undefined if not signed in
4748
public onAuthStateChanged(handler: (user: User) => void, error?: (err) => any, completed?: Unsubscribe): Unsubscribe {
4849
this.authStateChangedHandler = handler;
4950
if (error) this.authStateOnErrorHandler = error;
5051
console.log(">> added onAuthStateChanged handler");
51-
52+
handler(this.currentUser);
5253
return () => {
5354
this.authStateChangedHandler = undefined;
5455
this.authStateOnErrorHandler = undefined;

0 commit comments

Comments
 (0)