Home > Software engineering >  Resending Firebase auth verification emails when the user's email is no longer accessible
Resending Firebase auth verification emails when the user's email is no longer accessible

Time:06-26

So far in my project, I have set up a basic user management system. However, I'm unable to figure out a way to resend verification link after the user registers.

For example: When the user signs up, createUserWithEmailAndPassword is called which returns a promise. The promise is now resolved using then (or await) to which sendEmailVerification is called. This is all fine.

Note: The above flow is what I currently have implemented to for user management on the client side with Firebase Auth.

However, what if the user happens to delete this email or for whatever reason has no access to it at all. I want to be able to resend the link.

This uses Firebase Admin SDK on the backend and is an example of how to generate the verification email on the server-side. However, it appears that it is used in conjunction with account creation. In addition, it appears that Firebase Auth follows the same set of restrictions.

Not too sure where to go next and was wondering if there are any suitable workarounds.

Thanks.

CodePudding user response:

However, what if the user happens to delete this email or for whatever reason

Since the auth account requires a unique email address, that means you'd have to provide a way to change the email address. According to the documentation, the user must have signed in recently. If that's not the case then the user account is effectively dead and can't be recovered by the user. The user will need to sign up for a new account again with their new email address.

Alternatively, you might be able to write some code on your backend or development machine to change the email address using the admin SDK. The admin SDK can't be used on the frontend of your app.

CodePudding user response:

Add a link to the login page to resend the verification email.

Then, trigger something along these lines:

sendEmailVerification() async {
  await FirebaseAuth.instance.currentUser?.sendEmailVerification();
}

Another option is to check during the login process whether the user verified the email. If not, resend it. Along these lines:

  signInWithEmailAndPassword(
    String email,
    String password,
  ) async {
    try {
      final credential = await FirebaseAuth.instance
          .signInWithEmailAndPassword(email: email, password: password);

      if (credential.user!.emailVerified == false) {
        await _sendEmailVerification();
        return ... // not verified, but email sent
      }

      return ... // success
    } on FirebaseAuthException catch (e) {
      return ... // error
    } catch (e) {
      return ... // error
    }
  }
  • Related