Search code examples
flutterfirebase-authentication

Flutter Firebase Phone Authentication is not sending sms code. PhoneCodeAutoRetrievalTimeout method is called directly. Here is my code


I have just copy-pasted sample code to start working on Firebase Phone Authentication. But firebase is not sending SMS code to real numbers and test numbers. I tested for both. Can you help to find out the issue?

Here is the output for Flutter Doctor

Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 1.20.4, on Microsoft Windows [Version 10.0.19041.508], locale en-IN)
 
[√] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[√] Android Studio (version 4.0)
[√] VS Code (version 1.49.1)
[√] Connected device (1 available)

• No issues found!


    import 'package:firebase_auth/firebase_auth.dart';
    
    class AuthService {
      String _phoneNumber;
      String _verificationId;
      String _smsCode;
      String _message;
    
      final FirebaseAuth _auth = FirebaseAuth.instance;
    
      String get phoneNumber1 => _phoneNumber;
      String get verificationId => _verificationId;
      String get smsCode => _smsCode;
      String get message => _message;
    
      Future<void> verifyPhoneNumber(String phoneNumber) async {
        print(phoneNumber);
        PhoneVerificationCompleted verificationCompleted =
            (PhoneAuthCredential phoneAuthCredential) async {
          print('User Auto Logged In');
          // UserCredential _userCredential =
          //     await _auth.signInWithCredential(phoneAuthCredential);
        };
    
        PhoneVerificationFailed verificationFailed =
            (FirebaseAuthException authException) async {
          print('Auth Exception Occured');
          // throw Exception(authException.message);
          // _message =
          //     'Phone number verification failed. Code: ${authException.code}. Message: ${authException.message}';
        };
    
        PhoneCodeSent codeSent =
            (String verificationId, [int forceResendingToken]) async {
          print('SMS Sent');
          _verificationId = verificationId;
        };
    
        PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout =
            (String verificationId) async {
          print('SMS Auto Retrieval Timeout');
          _verificationId = verificationId;
        };
    
        await _auth.verifyPhoneNumber(
            phoneNumber: phoneNumber,
            timeout: const Duration(seconds: 5),
            verificationCompleted: verificationCompleted,
            verificationFailed: verificationFailed,
            codeSent: codeSent,
            codeAutoRetrievalTimeout: codeAutoRetrievalTimeout);
      }
    
      // Example code of how to sign in with phone.
      void signInWithPhoneNumber(String smsCode) async {
        final AuthCredential credential = PhoneAuthProvider.credential(
          verificationId: _verificationId,
          smsCode: smsCode,
        );
        final User user = (await _auth.signInWithCredential(credential)).user;
      }
    }

Output:


    I/BiChannelGoogleApi( 2258): [FirebaseAuth: ] getGoogleApiForMethod() returned Gms: com.google.firebase.auth.api.internal.zzaq@57888fb
    Reloaded 4 of 704 libraries in 1,013ms.
    I/flutter ( 2258): SMS Auto Retrieval Timeout

please help me to solve this issue.


Solution

  • The problem was with the firebase itself. It was not sending any sms that day. Now the same code is working fine.