Search code examples
firebaseflutterfirebase-authenticationflutter-dependenciessms-verification

How can I return variable from FirebaseAuth phone verification method in flutter?


I'm using FirebaseAuth phone verification, my problem is that I'm using bloc pattern so I want FirebaseAuth phone verification method to return some variable to the Bloc to indicate whether the user already exists or not as to navigate to phone verification screen or not.

Here is my phone verification method, note that this function is in another class not in the Bloc:

 static sendPhoneVerificationMessage(String phone) async {
    AuthCredential credential;
    final PhoneVerificationCompleted verificationCompleted = (AuthCredential user){
      print('Inside _sendCodeToPhoneNumber: signInWithPhoneNumber auto succeeded: '+user.toString());
    };

    final PhoneVerificationFailed verificationFailed = (AuthException authException){
      print('Phone number verification failed. Code: ${authException.code}. Message: ${authException.message}');
    };

    final PhoneCodeSent codeSent =
        (String verificationId, [int forceResendingToken]) async {
          verificationCode = verificationId;
      print("code sent to " + phone);
    };

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

      await FirebaseAuth.instance.verifyPhoneNumber(
          phoneNumber: phone,
          timeout: const Duration(seconds: 120),
          verificationCompleted: verificationCompleted,
          verificationFailed: verificationFailed,
          codeSent: codeSent,
          codeAutoRetrievalTimeout: codeAutoRetrievalTimeout);

  }

This is the function inside bloc that calls the previous function

  static sendPhoneVerificationMessage(String phone) async {
    String formattedPhone = "+2" + phone;
    await AuthAPI.sendPhoneVerificationMessage(formattedPhone);
  }

What I want is to perform some action based on the return from sendPhoneVerificationMessage function


Solution

  • I've used the FirebaseAuth phone verification with bloc pattern like you are trying to.

    In my case, I make the control with a Stream. I have an Enum that contains all possible states of the verification process and, for each state I call _stream.sink.add(MyEnum.CURRENT_STATE). (You can do this control in callbacks.)

    And finally, inside my view, the listeners change the UI according to the step passed to the stream.

    [free tips]
    Be careful with the timeout you are passing. If you pass a timeout different of zero, the App will try to get the verification code inside SMS automatically. If this happens, you will fall into verificationCompleted callback, your verification code will be invalidated automatically and you cannot use anymore.

    You can do the right control of this situation getting the user inside verificationCompleted method, or you can pass zero to the timeout and disable this behavior.