Home > OS >  flutter) Firebase Phone Auth's "verificationCompleted" does not work on Android devic
flutter) Firebase Phone Auth's "verificationCompleted" does not work on Android devic

Time:05-18

I authenticate through firebase phone auth, and on Android, I want to authenticate automatically through "verificationComplete". However, the function "codeSent" is called instead of "verificationCompleted".

  Future<void> _sendingCode(String phoneNum, BuildContext context) async {
FirebaseAuth auth = FirebaseAuth.instance;

return await auth.verifyPhoneNumber(
  phoneNumber: phoneNum,
  forceResendingToken: _forceResendingToken,

  verificationCompleted: (PhoneAuthCredential credential) async {
    logger.d('verificationCompleted - $credential');

    // Sign the user in (or link) with the auto-generated credential
    await auth.signInWithCredential(credential).then((value) async {

      // next page 
      context.read<PageController>().animateToPage(2,
          duration: Duration(milliseconds: 500), curve: Curves.ease);
    }).catchError((e) => logger.w(e));
  },

  codeAutoRetrievalTimeout: (String verificationId) {
    logger.w('timeout');
  },

  codeSent: (String verificationId, int? forceResendingToken) {
    logger.d("$phoneNum send code");

    _verificationId = verificationId;
    _forceResendingToken = forceResendingToken;
  },

  verificationFailed: (FirebaseAuthException error) {
    logger.e("Error: $error");
  },
);

}

Can I know the cause of this problem?

CodePudding user response:

onVerificationCompleted() will only be called when the phone number has been verified without any input from the user. To do what you are trying, you should be sending your action inside onCodeSent() instead.

Obtain phone number from user

  • Call PhoneAuthProvider.verifyPhoneNumber(auth) (as you are already) to send the pin to the user

  • onCodeSent() is called, with the verification ID and a resending token.

  • Inside of onCodeSent(), create an method to launch the "pin input screen" with the verification ID.

  • Get a pin from the user and then combine it with the verification ID by calling PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, userInput)

  • Use that credential to sign in the user using signInWithCredential(credential).

CodePudding user response:

You have to implement both functions. onVerificationCompleted function not working all the time.

More details about onVerificationCompleted link

Add the same logic inside your codeSent method.

  • Related