I am updating my current login email using verifyBeforeUpdateEmail() function from firebase authentication, which successfully updates the email (in the Authentication) upon user verification of the email link received in their inbox. So my task is once that is done, I want to update my firebase firestore with the new email. How is this possible – as I only want to update the firestore only when the user has succesffully verified it.
Code snippet as follows:
void updateEmail(
String newEmail, BuildContext context, UserModel userData) async {
FirebaseAuth auth = FirebaseAuth.instance;
User? user = auth.currentUser;
try {
await user!.verifyBeforeUpdateEmail(newEmail);
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text(
'You will shortly receive a confirmation link if you have entered a valid email. Please open your inbox and confirm'),
),
);
}
user = auth.currentUser;
if (user!.emailVerified) {
debugPrint('Only if the current user is verified');
UserModel newUserData = userData.copyWith(
mail: newEmail,
);
newUserData.updateFirestore();
}
} catch (e) {
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('$e'),
),
);
debugPrint(e.toString());
}
}
}
But with the above code regardless of new email verification, it gets updated in the firebase firestore (which I don’t want).