This repository was archived by the owner on Apr 4, 2023. It is now read-only.
File tree 2 files changed +32
-2
lines changed
2 files changed +32
-2
lines changed Original file line number Diff line number Diff line change @@ -62,7 +62,8 @@ const firebaseWebApi = require("nativescript-plugin-firebase/app");
62
62
As stated [ here] ( https://firebase.google.com/docs/auth/ios/manage-users#get_the_currently_signed-in_user ) :
63
63
64
64
> 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 >
66
67
To listen to auth state changes you can register a listener during ` init ` :
67
68
68
69
``` js
@@ -98,7 +99,35 @@ If - for some reason - you want more control over the listener you can use these
98
99
99
100
// check if already listening to auth state changes
100
101
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));
101
129
```
130
+ </details >
102
131
103
132
### Get Current User
104
133
Once the user is logged in you can retrieve the currently logged in user:
Original file line number Diff line number Diff line change @@ -44,11 +44,12 @@ export namespace auth {
44
44
}
45
45
46
46
// 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
47
48
public onAuthStateChanged ( handler : ( user : User ) => void , error ?: ( err ) => any , completed ?: Unsubscribe ) : Unsubscribe {
48
49
this . authStateChangedHandler = handler ;
49
50
if ( error ) this . authStateOnErrorHandler = error ;
50
51
console . log ( ">> added onAuthStateChanged handler" ) ;
51
-
52
+ handler ( this . currentUser ) ;
52
53
return ( ) => {
53
54
this . authStateChangedHandler = undefined ;
54
55
this . authStateOnErrorHandler = undefined ;
You can’t perform that action at this time.
0 commit comments