Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 386
Marco Fregoso Avatar asked Apr 02 '19 01:04

Marco Fregoso


People also ask

Can we send OTP using Firebase?

Stay organized with collections Save and categorize content based on your preferences. You can use Firebase Authentication to sign in a user by sending an SMS message to the user's phone. The user signs in using a one-time code contained in the SMS message.

How do I change the SMS verification template in Firebase phone Auth?

Neither the email verification template nor the SMS verification template can be modified. You can select the language from the Firebase console, however this is a per project setting and you can't modify the templates.


1 Answers

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

like image 75
Vineet Avatar answered Oct 14 '22 18:10

Vineet