Here is my code on profile page, this works fine first time when i redirect from login method of AuthService
const user = firebase.auth().currentUser;
if (user != null) {
this.name = user.displayName;
this.uid = user.uid;
} else {
this.name = "Unknown";
}
However, if I refresh the page or go to any other page and come back to this profile page, currentUser becomes null.
Here is my auth service code.
import * as firebase from "firebase/app";
import { auth } from "firebase/app";
async googleSignin() {
firebase
.auth()
.setPersistence(firebase.auth.Auth.Persistence.LOCAL)
.then(async () => {
const provider = new auth.GoogleAuthProvider();
const credential = await this.afAuth.auth.signInWithPopup(provider);
this.updateUserData(credential.user);
this.router.navigate(["/profile"]);
return;
});
}
Why i am loosing the currentUser? I even added Persistence.LOCAL too.
You can also get the currently signed-in user by calling CurrentUser . If a user isn't signed in, CurrentUser returns null. Note: CurrentUser might also return null because the auth object has not finished initializing.
The underlying ID token that authenticates refreshes every hour. So, after an account is deleted, the client will be authenticated for up to one hour. Only until it tries to refresh the ID token will the client become unauthenticated.
Firebase users have a fixed set of basic properties—a unique ID, a primary email address, a name and a photo URL—stored in the project's user database, that can be updated by the user (iOS, Android, web).
In fact, the Firebase Auth SDK requires at least one, and possibly two steps, to take in order to deliver a valid current user object: Check and load the persisted user ID token from disk. Refresh the user’s ID token if needed (hourly).
This means that the SDK is not going to block the main thread of execution in order to deliver data — the object containing the currently signed in user is no exception. In fact, the Firebase Auth SDK requires at least one, and possibly two steps, to take in order to deliver a valid current user object:
First two lines successfully return FirebaseUser. The third returns null. Forth, if third is commented throws a null reference error.
Here’s some Kotlin you can use to build a LiveData that exposes Firebase Auth user state: This code creates an extension function on FirebaseAuth that lets you build a new LiveData that emits a sealed class with the three possible states. This LiveData could be a singleton in your app that any component can use to track user state.
When you navigate to a new page, you're reloading the Firebase Authentication SDK. At this point Firebase automatically refreshes the authentication state of the current user, but this may require a roundtrip to the server. And by the time your `firebase.auth().currentUser runs that refresh apparently isn't done yet.
For this reason you should use onAuthStateChange
to listen for changes, as shown in the documentation on getting the currently signed in user:
firebase.auth().onAuthStateChanged(function(user) {
if (user != null) {
this.name = user.displayName;
this.uid = user.uid;
} else {
this.name = "Unknown";
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With