I have written a flutter app with firebase auth. If the user has not verified his email, a banner should be displayed. On this banner there should be a button on which you can resend the email. this also works. however, i have the problem that even if i confirm the email address in this email, the app still says that the email has not been confirmed. How can I fix this?
The banner:
if(FirebaseAuth.instance.currentUser!.emailVerified == false)
Container(
height: 40,
width: double.infinity,
color: Colors.red,
child: Row(
children: [
const Padding(
padding: EdgeInsets.symmetric(horizontal: 8.0),
child: Icon(Icons.error, color: Colors.white),
),
const Text("please confirm your Email Adress", style: TextStyle(color: Colors.white, fontSize: 16, fontWeight: FontWeight.bold)),
const Spacer(),
TextButton(
onPressed: () async {
FirebaseAuth.instance.currentUser!.sendEmailVerification();
CustomSnackBar.showSnackBar(context, "Email gesendet");
},
child: const Text("Resend", style: TextStyle(color: Colors.white, fontSize: 16, fontWeight: FontWeight.bold)),
),
],
),),
2
Answers
The solution is to call
.reload()
on the currentUser. With this the satus will get updated and everything will work fine. The code i use to do this is:Sending an email does not immediately verify that email address, as that’d be a pretty bad security risk.
In order to verify the email address, the user has to click a link in the email that they receive. And since they will do that from their mail app, your Flutter app won’t automatically be informed when this happens.
Whether the user’s email address is verified is part of their ID token, which the Firebase SDK refreshes once per hour. If you don’t want to wait that long, you can force a reload of the token too. But you will have to determine when to do this, as the SDK has no way of knowing that the user clicked the link.
Also see (not all for Flutter, but the approach is the same across platforms):