Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get firebase.auth().currentUser with async/await

I successfully check user's authentication state with onAuthStateChange observer and redirect user to the Dashboard page (react-native project). However, on the dashboard I already want to show some user specific data, e.g. enrolled programs/statistics. For that I need currentUser object to be initialised and populated, which takes some time (I need uid from there to get some data from Database). Thus, i'm looking for some way to wait until this process finishes successfully. What I'm trying to do it to use async/await syntax in componentDidMount, but the result returned is null. (Same syntax successfully works in other screens, but fails in the first one)

async componentDidMount() {
  try {
    let uid = await firebase.auth().currentUser.uid;
    console.log(uid);
  } catch(error) {
    console.log(error);
  }
}

What could be the best way to wait for currentUser object being loaded using async/await syntax? I believe that the reason could be that firebase returns null as the first result and then correct uid as the second one.

For now for the workaround, I'm using simple setInterval function, but I do not want to increase amount of loading time that much.

  let waitForCurrentUser = setInterval(() => {
    if ( firebase.auth().currentUser.uid !== null ) {
        clearInterval(waitForCurrentUser);
        let uid = firebase.auth().currentUser.uid;
        this.setState({uid});
        return firebase.auth().currentUser.uid;
    } else {
      console.log('Wait for it');
    }
  }, 700);
like image 960
Denis Avatar asked Jan 31 '26 13:01

Denis


1 Answers

In Firebase v10, by utilizing the authStateReady method, which returns a promise, we can determine when the auth object is populated with the currentUser:

1. With promise.then() :

import { getAuth } from 'firebase/auth';

// In your component:
  useEffect(() => {
    const auth = getAuth();
    auth.authStateReady().then(() => {
      // Do whatever you want here ...

      // E.g. checking whether the user is logged in or not:
      setIsAuthReady(true);
      if (auth.currentUser) {
        setIsLoggedIn(true);
      }
      // End of E.g.

    });
  }, []);

2. With await promise :

Utilizing the await keyword is also an option, given that the response is a promise:

import { getAuth } from 'firebase/auth';

// In your component:
  useEffect(() => {
    const auth = getAuth();

    const checkAuthState = async () => {
      await auth.authStateReady();
      // Do whatever you want here ...

      // E.g. checking whether the user is logged in or not:
      setIsAuthReady(true);
      if (auth.currentUser) {
        setIsLoggedIn(true);
      }
      // End of E.g.
    };

    checkAuthState();
  }, []);
like image 187
Iman Mahmoudinasab Avatar answered Feb 02 '26 03:02

Iman Mahmoudinasab



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!