Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

After refresh, firebase Auth current user returns null

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.

like image 709
NetMagician Avatar asked Oct 07 '19 00:10

NetMagician


People also ask

What does firebase auth () CurrentUser return?

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.

Why firebase user still signed in after I deleted it from firebase dashboard?

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.

Does firebase Auth store user data?

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).

How do I get current user object in Firebase Auth SDK?

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).

Does Firebase Auth SDK block the main thread of execution?

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:

Which two lines successfully return firebaseuser?

First two lines successfully return FirebaseUser. The third returns null. Forth, if third is commented throws a null reference error.

How do I build a livedata with Firebase Auth?

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.


1 Answers

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";
  }
});
like image 113
Frank van Puffelen Avatar answered Sep 20 '22 23:09

Frank van Puffelen