5

I have a an app built around a StreamBuilder listening for FirebaseAuth.instance.onAuthStateChanged

  return new StreamBuilder<FirebaseUser>(
  stream: FirebaseAuth.instance.onAuthStateChanged,
  builder: (BuildContext context, snapshot) {
    if (snapshot.connectionState == ConnectionState.waiting) { ...

I'm trying to reload the user and use onAuthStateChanged to pick up when an email is verified. User.reload() is insufficient with the StreamBuilder. It's a similar issue to this onAuthStateChanged doesn't get called when email is verified in flutter

I thought a solution would be to force a logout and login but I'm at a loss on how to do that - e.g. step 2, login again to trigger onAuthStateChanged.

  _auth.signOut(); //sign out and immediate sign in
  final FirebaseUser user = await _auth.signIn----( //which signin function? 
user1961
  • 1,270
  • 2
  • 17
  • 27
  • 1
    Did you find a solution? I am having the same issue in flutter – Adim Oct 21 '19 at 14:43
  • No. What I ended up doing was forcing the user to logout if he wasn't verified. Then after he verifies he can login again. – user1961 Oct 22 '19 at 15:14

2 Answers2

0

Have you tried refreshing auth token?

https://firebase.google.com/docs/auth/admin/manage-sessions

Andrea
  • 65
  • 6
0

Use _auth.userChanges() stream instead of _auth.onAuthStateChanges()

return StreamBuilder(
      stream: _auth.userChanges(),
      builder: (ctx, userSnapshot) {
        User? user = _auth.currentUser;
        if (userSnapshot.hasData && user != null && user.emailVerified) {
           // load screen on successful login
        }
        // fallback to auth screen
  },
);

and on successful verification call refreshToken in your verification method

Future<void> verifyEmail() async {
    user = _auth.currentUser;
    await user!.reload();
    if (user!.emailVerified) {
      setState(() {
        _isEmailBeingVerified = false;
      });
      user!.refreshToken;
    }
  }