How check if user verify mail and then let logged in?

Issue

I know the question was asking a lot of times but after spending hours trying to understand im still don’t know how to doing that.

So right know I got 2 Future methods

the SigIn

 Future<String> signIN(String email, String password) async {
    
      try {
        (await FirebaseAuth.instance.signInWithEmailAndPassword(
          email: email.trim(),
          password: password,
        ))
            .user;
      } on FirebaseAuthException catch (e) {
        switch (e.code) {
          case 'invalid-email':
            {
              return 'Email is not valid';
            }
          case 'user-disabled':
            {
              return 'Account is not active';
            }
          case 'user-not-found':
            {
              return 'No user found';
            }
          case 'wrong-password':
            {
              return 'wrong password';
            }
          default:
            {
              return 'Unexpected error!';
            }
        }
      }
      return null;

    
  }

And the Sign up

 Future<String> signUp(String email, String password) async {
    try {
      (await FirebaseAuth.instance.createUserWithEmailAndPassword(
        email: email.trim(),
        password: password,
      ))
          .user
          .sendEmailVerification();
    } on FirebaseAuthException catch (e) {
      switch (e.code) {
        case 'invalid-email':
          {
            return 'Email is not valid';
          }
        case 'user-disabled':
          {
            return 'Account is not active';
          }
        case 'user-not-found':
          {
            return 'No user found';
          }
        case 'wrong-password':
          {
            return 'wrong password';
          }
        default:
          {
            return 'Unexpected error!';
          }
      }
    }
    return null;
  }

And i wish I know how to first check if user verified mail and then let him login.
Right know user press register button and automatically login .

Please help!!

Here the on pressed methods

First the pressed register button

      onPressed: () async {
            if (_formKey.currentState.validate()) {
              
              String authError = await _auth.signUp(email, password);
              if (authError != null) {
                 setState(() => jawoll = true);
                setState(() => error = authError);
               
              }else{
                 setState(() => loading = true);
                 setState(() => jawoll = false);
                setState(() => error = "Email send to $email");
             
              }
            }
          }

And the sign in in button

   onPressed: () async {
              if (_formKey.currentState.validate()) {
                
                String authError = await _auth.signIN(email, password);
                if (authError != null) {
                  setState(() => error = authError);
                  print("olaaa");
                  print(error);
                }
                setState(() => loading = false);
              }
            }

Solution

Remove the stream (authStateChanges) from your widget tree that is responsible for automatically logging in the user.

Then, manually push a new screen for email registration. After it’s done let your user go to the home screen.

On opening the app again in the future, you can check if the user is logged in and if the email is verified. If so, move to the home screen and if not to the log in screen.

The problem here is that the stream automatically changes on user login, wether the email is verified or not. So, remove it and proceed !!

Answered By – Kushal Goel

Answer Checked By – Jay B. (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.