how to verify email and password flutter firebase

Viewed 1142

i'm new with flutter and i want to implement a log in screen on my application. for that i use flutter and firebase. the problem is that when anything in the email and password field the application access to the home. so i need to verify the email before get the access.

here is the code

thanks guys for help me,

               onPressed: () {
                if (_formKey.currentState.validate()) {
                  Future<String> check = logIn();
                  if (check != null) {
                    Navigator.pushReplacement(
                        context,
                        MaterialPageRoute(
                            builder: (BuildContext context) =>
                                HomePage()));
                  }
                }
              
1 Answers

Firebase offers a function for checking if users are verified which I used in the code below.

Future<bool> login(
  String email, String password) async {
final user = (await FirebaseAuth.instance
        .signInWithEmailAndPassword(email: email, password: password))
    .user;
if (user.isEmailVerified) {
  return true;
}
return false;
}

However, to use this function, you have first send an email verification to your users somewhere in the function you call when your users create an account. The function below will create the user and send a verification email and then return a FirebaseUser when done.

Future<FirebaseUser> register() async {
await _auth
    .createUserWithEmailAndPassword(email: email.trim(), password: password)
    .then(
  (result) async {
    //send verifcation email
    result.user.sendEmailVerification();
    return result.user;
  },
);
return null;
}
Related