androidfirebasedartflutterfirebase-authentication

Firebase Phone Auth isn't sending SMS with code


I'm trying to set up phone number authentication using Firebase for my Android app. I've enabled phone authentication on the "authentication" tab on Firebase console and added my number for testing. I've tried with multiple numbers but I get no SMS sent to any of them despite the console telling me PhoneCodeSent function was fired and displaying a success message. I've tried to fix it for a while and can't seem to find a solid answer. What's wrong with my code?

My code is the following

...
...
...
String verificationId;
int resendingToken;

Future<void> _sendConfirmationCode() async {
if (formKey.currentState.validate()) {
  formKey.currentState.save();

  final PhoneVerificationCompleted verificationCompleted = (FirebaseUser user) {
    setState(() {
      print('verification has been completed');
    });
  };

  final PhoneVerificationFailed verificationFailed = (AuthException authException) {
    setState(() {
      print(countrySelected);
      print(this.phone + "\n");
      print('verification failed error: ' + authException.message);}
    );
  };

  final PhoneCodeSent codeSent = (String verificationId, [int forceResendingToken]) async {
    this.verificationId = await verificationId;
    this.resendingToken = await forceResendingToken;
    print("code sent: "+ verificationId);
  };

  final PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout =
      (String verificationId) {
    this.verificationId = verificationId;
    print("time out");
  };

  if (this.phone.isNotEmpty) {

    await FirebaseAuth.instance.verifyPhoneNumber(
      phoneNumber: "<PHONE NUMBER HARDCODED HERE>", //I've tried hardcoding my number too but it didn't work
      timeout: const Duration(seconds: 5),
      verificationCompleted: verificationCompleted,
      verificationFailed: verificationFailed,
      codeSent: codeSent,
      forceResendingToken: resendingToken,
      codeAutoRetrievalTimeout: codeAutoRetrievalTimeout);
  }
 }
}

The following gets printed to the console:

I/flutter (32425): code sent: AM5PThC4JnFK7czWDoAdqSFjBdDk5oq9VwufNvWxgcOg4fEgbHE8CoYGuWMCjzTnfPbOlpcdfefouwL86dsD5fQs73CcR3NgvI2SRqHEHgM0n34yqqJma75ZCvPGMeTmwy6XDCA9-P0p
I/flutter (32425): time out

I've also tried waiting a few hours in case I'd reached Firebase's SMS limit, but that doesn't seem to be the problem. Any ideas?


Solution

  • When you add a phone number for testing, you are basically whitelisting it. Whitelisted phone numbers are used for testing phone number authentication without sending an actual SMS message.

    No verification code SMS is sent to whitelisted phone numbers and a default OTP of 123456 (unless you have provided the 6-digit verification code for that specific number when adding to the test console). Try to verify using this code, it will authenticate and generate a valid verification ID.

    For getting the SMS, remove your number from the whitelist and try again.

    Reference: https://firebase.google.com/docs/auth/android/phone-auth#test-with-whitelisted-phone-numbers