3

I am facing a strange issue of some phone numbers do not receive OTP without receiving any errors, just nothing happens. Almost all the users are receiving the OTP just fine, except for only very few. How can I debug the issue? Does Firebase Auth have some log with any errors? Is my code missing something?

Here is the code (in Flutter) for sending the OTP

sendOtp(String number, BuildContext context) async {
    print("Sending OTp");
    state = ViewState.Busy;
    await auth.verifyPhoneNumber(
      phoneNumber: '+966$number',
      timeout: Duration(seconds: 30),
      verificationCompleted: (AuthCredential credential) {
        print("Verified");
        signInWCredential(credential, context, number);
      },
      verificationFailed: (FirebaseAuthException e) {
        print("verification error code is: ${e.code}");
        print("verification error message is: ${e.message}");
        if (e.code == "too-many-requests") {
          Dialogs.dialog(
                  context,
                  "${Localization.of(context).error}!",
                  Localization.of(context).tooManyOtpRequestsError,
                  Localization.of(context).ok,
                  isDoublePop: true)
              .then((value) {
            if (value == ConfirmAction.ACCEPT) {
              Navigator.of(context).pop();
            }
          });
        } else {
          FirebaseCrashlytics.instance.recordError(e, StackTrace.current);
          print("Error is: $e");
          Dialogs.dialog(
              context,
              "${Localization.of(context).error}!",
              Localization.of(context).errorSendingOtp,
              Localization.of(context).ok,
              isDoublePop: true);
        }
      },
      codeSent: (String verificationId, [int resendToken]) {
        print("Code sent");
        print("verification ID is: $verificationId");
        setVerificationId(verificationId);
        print("Resend token is: $resendToken");
        state = ViewState.Idle;
      },
      codeAutoRetrievalTimeout: (String verificationId) {},
    );
  }
halahmadi
  • 125
  • 2
  • 12

3 Answers3

1

I have contacted GCP support to investigate this issue. They reported to me that the SMS delivery in my customers' region has 85%-90% success rate. They are working with SMS providers to solve that issue.

I made a new feature request to return an error message if the SMS delivery fails. Meanwhile, I solved this issue by creating my own custom authentication API and sending SMS through another service provider.

halahmadi
  • 125
  • 2
  • 12
0
import 'package:firebase_auth/firebase_auth.dart';  
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class Demo extends StatefulWidget {
  @override
  _DemoState createState() => _DemoState();
}

class _DemoState extends State<Demo> {
  TextEditingController _controller = TextEditingController();

Future<void> _showMyDialog() async {
return showDialog<void>(
  context: context,
  barrierDismissible: false, // user must tap button!
  builder: (BuildContext context) {
    return AlertDialog(
      title: Text('AlertDialog Title'),
      content: SingleChildScrollView(
        child: ListBody(
          children: <Widget>[
            Text('This is a demo alert dialog.'),
            TextField(
              controller: _controller,
            )
          ],
        ),
      ),
      actions: <Widget>[
        TextButton(
          child: Text('Approve'),
          onPressed: () {
            Navigator.of(context).pop();
          },
        ),
      ],
    );
  },
);
 }

  void sendOTP() async {
await FirebaseAuth.instance.verifyPhoneNumber(
  phoneNumber: "your number",
  verificationCompleted: (PhoneAuthCredential credential) async {
    print("verificationCompleted");
    await FirebaseAuth.instance.signInWithCredential(credential);
  },
  verificationFailed: (FirebaseAuthException e) {
    print("FirebaseAuthException");
    print(e.code);
  },
  codeSent: (String verificationID, int token) async {
    print("codeSent");
    await _showMyDialog();
    PhoneAuthCredential phoneAuthCredential = PhoneAuthProvider.credential(
        verificationId: verificationID, smsCode: _controller.text);
    await FirebaseAuth.instance.signInWithCredential(phoneAuthCredential);
    print("completed");
  },
  codeAutoRetrievalTimeout: (String verificationID) {
    print("$verificationID");
  },
  timeout: const Duration(seconds: 60),
);
 }

  @override
  Widget build(BuildContext context) {
return Scaffold(
  body: Center(
    child: FlatButton(
      child: Text(
          "send otp"),
      onPressed: sendOTP,
    ),
  ),
);
 }
}

this is my Code for Phone Authentication using firebase and it is working fine for many mobile numbers

0

I faced this issue and found out from searching the internet that Firebase's Phone Authentication does not work for users who ported their number from one network to another network. I also found out that other phone authentication SDKS (other than Firebase) has the same issue. The only solution is to give your users the option to sign in with email and password or another sign-in method other than phone authentication. Offering both options will guarantee that user can sign in.

Axes Grinds
  • 756
  • 12
  • 24