Flutter Firebase Auth Phone verification sms code issue

Viewed 837

i'm trying to send user a verification sms code using flutter FirebaseAuth instance, below is a snapshot of the code

pubspec.yml firebase_auth: ^1.2.0

  Future<void> sendPhoneVerificationCode(String phoneNumber) async {
    await FirebaseAuth.instance.verifyPhoneNumber(
      autoRetrievedSmsCodeForTesting: phoneNumber,
      phoneNumber: '+267$phoneNumber',
      timeout: Duration(seconds: 60),
      verificationCompleted: (phoneAuthCredential) {
        this.smsCode = phoneAuthCredential.smsCode;
        this.verificationId = phoneAuthCredential.verificationId;

        logger.w(
          'verification smsCode ${this.smsCode}',
        );
      },
      verificationFailed: (error) {
        if (error.code == 'invalid-phone-number') {
          errorMessage = 'The provided phone number is not valid.';
        } else {
          errorMessage = error.message;
        }
      },
      codeSent: (verificationId, [forceResendingToken]) {
        this.verificationId = verificationId;
        logger.w('verificationId is $verificationId');
      },
      codeAutoRetrievalTimeout: (String verificationId) {
        this.verificationId = verificationId;
      },
    );
  }

Instead of getting an sms code from verificationCompleted method, i'm getting phone number, How can i get sms code sent to the user?

1 Answers

Here's an example repo:- demo repo

sendPhoneVerificationCode function :-

Future<void> _submitPhoneNumber() async {
    /// NOTE: Either append your phone number country code or add in the code itself
    /// Since I'm in India we use "+91 " as prefix `phoneNumber`
    String phoneNumber = "+91 " + _phoneNumberController.text.toString().trim();
    print(phoneNumber);

    /// The below functions are the callbacks, separated so as to make code more readable
    void verificationCompleted(AuthCredential phoneAuthCredential) {
      print('verificationCompleted');
      ...
      this._phoneAuthCredential = phoneAuthCredential;
      print(phoneAuthCredential);
    }

    void verificationFailed(AuthException error) {
      ...
      print(error);
    }

    void codeSent(String verificationId, [int code]) {
      ...
      print('codeSent');
    }

    void codeAutoRetrievalTimeout(String verificationId) {
      ...
      print('codeAutoRetrievalTimeout');
    }

    await FirebaseAuth.instance.verifyPhoneNumber(
      /// Make sure to prefix with your country code
      phoneNumber: phoneNumber,

      /// `seconds` didn't work. The underlying implementation code only reads in `milliseconds`
      timeout: Duration(milliseconds: 10000),

      /// If the SIM (with phoneNumber) is in the current device this function is called.
      /// This function gives `AuthCredential`. Moreover `login` function can be called from this callback
      verificationCompleted: verificationCompleted,

      /// Called when the verification is failed
      verificationFailed: verificationFailed,

      /// This is called after the OTP is sent. Gives a `verificationId` and `code`
      codeSent: codeSent,

      /// After automatic code retrival `tmeout` this function is called
      codeAutoRetrievalTimeout: codeAutoRetrievalTimeout,
    ); // All the callbacks are above
  }
Related