2
votes

I'm having the problem of sending the email verification link to a user while they are trying to register an account that was already created. I am coding with Flutter with Firebase as the back-end database.


final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;

   abstract class BaseAuth {
     Stream<String> get onAuthStateChanged;
     Future<String> currentUser();
     Future<String> resendVerifyEmail(BaseAuth auth, String email);
   } 

   class Auth implements BaseAuth {...}

   Future<String> currentUser() async {
      final FirebaseUser user = await _firebaseAuth.currentUser();
      print("Getting current user: " + user?.uid);
      return user?.uid;
   }

  Future<String> resendVerifyEmail(BaseAuth auth, String email) async {
    try {
      final FirebaseUser user = await _firebaseAuth.currentUser();
      user.sendEmailVerification();
      return user?.uid;
    } catch (e) {
      print("An Error occurred while sending verification link");
      print(e.toString());
      return null;
    }
   }

Inside the try block, when calling the method currentUser(), the code exits and returns to the method that calls resendVerifyEmail without finishing the try-catch block.

I am having difficulty getting the email verification to resend inside of the popup that call resendVerifyEmail.

Please let me know any alternative ways to send email verification from Firebase with Flutter or what to do with this.

  void _showVerifyEmailDialog(BuildContext context, String email) {
final BaseAuth auth = AuthProvider.of(context).auth;
showDialog(
  context: context,
  builder: (BuildContext context) {
    // return object of type Dialog
    return AlertDialog(
      title: new Text("Account Already Exists"),
      content: new Text("Please verify account in the link sent to email"),
      actions: <Widget>[
        new FlatButton(
          child: new Text("Resend verification email"),
          onPressed: () {
            Navigator.of(context).pop();
            auth.resendVerifyEmail();   <- Called from this line in login.dart

Why is the code exiting at the statement below without moving to next line of sending email?

final FirebaseUser user = await _firebaseAuth.currentUser();
1

1 Answers

2
votes

Seems like you are adding unnecessary items in your resendemail function. Here is how I implemented my own:

  Future<void> sendEmailVerification() async {
    final FirebaseUser user = await _firebaseAuth.currentUser();
    user.sendEmailVerification();
  }

Simply call it in after registering the user like this:

  await Auth().sendEmailVerification();
  await Auth().signOut();

Make sure the sign the user out so that they can't automatically login when they close and open your app. You should add checks as well in your login if the user has his email verified like this:

  Future<bool> isEmailVerified() async {
    final FirebaseUser user = await _firebaseAuth.currentUser();
    return user.isEmailVerified == null ? false : user.isEmailVerified;
  }

Just call it when you log the person in.