Firebase, Flutter: Can I check a validity of PhoneAuthCredential before signInWithCredential?

Viewed 182

I want to link phone and email auth in a Flutter project using FirebaseAuth.

Steps are below:

auth = FirebaseAuth.instance;
  1. Get user's phone number and send sms code using auth.verifyPhoneNumber

  2. Get sms code input from user and send to

    PhoneAuthCredential credential = PhoneAuthProvider.credential(
           verificationId: verificationId,
           smsCode: smsCode,
         );
    
  3. pass the 'credential' to email register page and combine with an authResult like this:

    final UserCredential authResult = await _auth
               .createUserWithEmailAndPassword(email: email, password: password);
    
    await authResult.user.linkWithCredential(phoneCredential);
    

The thing is, I want to check if user's sms code input is valid or not before getting the email register page. The only way I've found to check this is put this credential to auth.signInWithCredential(credential). But I want to avoid this, because the other pages listen to onAuthChanged so that it can react if any sign-in or out occurs.

What would be the best way to solve this problem?

Thanks in advance.

1 Answers

Try using FirebaseUser.updatePhoneNumberCredential instead, since it does not trigger onAuthChanged.

Related