r08y
r08y

Reputation: 125

Firebase Reactjs get current user from context

I'm new of Reactjs and I'd like to understand if the only way to get Firebase currentUser from context is through onAuthStateChanged.

I try to be more clear: let's say I signed-in by a "SignIn" component by using Firebase context and, after signed-in, I need to get currentUser in a different component, it seems to me the only way to get it is something like:

const [user, setUser] = useState(null);

...

 useEffect(() => {
    props.firebase.auth.onAuthStateChanged(authUser => {
      if (!!authUser) {
        setUser(authUser);
      } 
    }); 
  }, []);

Is it true?

Thanks

Upvotes: 1

Views: 1723

Answers (2)

r08y
r08y

Reputation: 125

Thanks to who reply my question, I found the answer in this detailed article.

Upvotes: 0

Joe Lloyd
Joe Lloyd

Reputation: 22323

You can call on a current user any time

You can use this auth function

const user = firebase.auth().currentUser;

You don't have to store it. Unless you want to.

Upvotes: 2

Related Questions