1

When authenticating in firebase using the signInWithEmailAndPassword() API, I am unable to get a valid "current" user.

  1. I sign into the application with user name and password
auth.signInWithEmailAndPassword() 
  1. I once signed in the user is taken to a Home page

  2. I attempt to get the user's UID but there is no valid user returned. The call happens in an async function as such:

currentUser = await auth.currentUser(); 

currentUser is null.

Any ideas what could be causing this?

4 Answers 4

5

The documentation suggests that you use an auth state observer to get a callback when the user is signed in and a User object is available. As shown in that linked documentation:

FirebaseAuth.instance .authStateChanges() .listen((User user) { if (user == null) { print('User is currently signed out!'); } else { print('User is signed in!'); } }); 

I strongly suggest not relying on currentUser, as that can only check at a moment in time. The observer is guaranteed to let you know when the user object is available for use.

Sign up to request clarification or add additional context in comments.

Comments

0

after getting user just reload it once it will solve your problem code for reference :D

final FirebaseAuth _auth = FirebaseAuth.instance; User currentuser; bool isloggedin= false; getUser() async{ User user = _auth.currentUser; await user?.reload(); user = _auth.currentUser; if(user !=null) { setState(() { this.currentuser = user; this.isloggedin=true; }); } } 

Comments

-1

What if you reload the returned FirebaseUser? This worked for me.

const User currentUser = await auth.currentUser; if (currentUser != null) { await currentUser.reload(); } 

Clearly simplified code. Note: auth.currentUser() is now auth.currentUser.

This might be a duplicate: Flutter Firebase Authentication currentUser() returns null.

1 Comment

This answer isn't applicable here. The question states that the first call to auth.currentUser returns null. Therefore, the code suggested in this solution--calling currentUser.reload()--will never be executed.
-1

try this code

FirebaseAuth.instance.currentUser?.providerData[0].email 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.