Carlos
Carlos

Reputation: 91

Firebase AuthProvider Flutter

I'm currently building a Futter app with Firebase and I'm struggling because whenever a user logs in or out, the state auth notifier will get trigger and work as intended but it does not notify changes such as whenever a user verifies his email, changes his display name or changes his email. It does not even notify when a user converts his anonymous account to an email account.

The notifier code is:

final FirebaseAuth _firebaseAuth;
Stream<User?> get authStateChanges => _firebaseAuth.authStateChanges();

and

final firebaseUser = context.watch<User?>();

Upvotes: 1

Views: 506

Answers (1)

Stewie Griffin
Stewie Griffin

Reputation: 5638

authStateChanges notifies about only the user's sign-in state such as sign-in or sign-out, not the ones you mention like changes to the User profile. Nevertheless, you can use userChanges() method to listen on changes to any user updates:

Notifies about changes to any user updates.

This is a superset of both authStateChanges and idTokenChanges. It provides events on all user changes, such as when credentials are linked, unlinked and when updates to the user profile are made. The purpose of this Stream is for listening to realtime updates to the user state (signed-in, signed-out, different user & token refresh) without manually having to call reload and then rehydrating changes to your application.

However, it does not notify about email verification. As it stated in the docs, events are fired when the following occurs:

  • Right after the listener has been registered.
  • When a user is signed in.
  • When the current user is signed out.
  • When there is a change in the current user's token.
  • When the following methods provided by FirebaseAuth.instance.currentUser are called:
    • reload()
    • unlink()
    • updateEmail()
    • updatePassword()
    • updatePhoneNumber()
    • updateProfile()

Also, userChanges() will not fire if you update the User profile via your own firebase admin sdk implementation.

You can force a reload using the following FirebaseAuth.instance.currentUser.reload() to retrieve the latest User profile.

Upvotes: 1

Related Questions