Search code examples
firebaseflutterdartfirebase-authenticationemail-verification

How to Login and Register CORRECTLY in Firebase with Flutter?


I have this app that do login and register with firebase + email verification but not very effecient as when the user register if he entered the username, email , password, ..... It register even if the email is wrong so I searched and found a solutoin which is email varification in firebase but the problem is that if the user didn't verify, Firebase register it and he can login (even if the email is not valid), so any ideas ? also new to flutter. My SignUp code Which I excute in the signup page:

static Future signup(String email, String password, BuildContext ctx) async {
    String emessage;
    if (email == '' || password == '') {
      emessage = 'Please Provide Valid Email And Password';
    } else {
      emessage = 'An Error Occurred. Please Try Again Later.';
    }
    try {
      final FirebaseUser user = (await _auth.createUserWithEmailAndPassword(
        email: email,
        password: password,
      ))
          .user;
      user.sendEmailVerification();
      return user.uid;
    } catch (e) {
      showDialog(
          context: ctx,
          builder: (BuildContext context) {
            return AlertDialog(
              title: Text('Error'),
              content: Text(emessage),
              actions: [
                FlatButton(
                  onPressed: () => Navigator.pop(context),
                  child: Text('Ok'),
                ),
              ],
            );
          });
    }
  }

Solution

  • The firebase docs of createUserWithEmailAndPassword do not list any parameters to force upfront email verification.

    You could

    1. Use signInWithEmailLink and add a password after the first login.
    2. Disallow users without a verified email address to access content in your app -- just like you do it with unauthenticated users.