Phone Auth with Firebase Flutter

Asked

Viewed 737 times

0

I’m trying to set up Phone Auth on Flutter but I can’t find anything useful for my case.

Would anyone have any solution to this?

I already checked the data firebase SHA1 app package name and it’s all correct, I downloaded the updated google-services, updated firebase_auth but I still have this problem, someone who went through the same problem would have the solution?

I am having these errors when I try to send the code to the number informed:

2019-06-29 16:54:24.061 13560-13560/com.zotinfo.diskhamburguerapp W/BiChannelGoogleApi: [FirebaseAuth: ] getGoogleApiForMethod() returned Gms: com.google.firebase.auth.api.internal.zzal@95837bb
2019-06-29 16:54:24.513 13560-13584/com.zotinfo.diskhamburguerapp I/flutter: Phone number verification failed. Code: firebaseAuth. Message: This app is not authorized to use Firebase Authentication. Please verifythat the correct package name and SHA-1 are configured in the Firebase Console. [ App validation failed ]
  • How strange if you have already checked all the data. Did you also confirm the firebase startup settings? Other firebase things work normally?

  • 1

    Yes I already got it working, there was a configuration missing, the debug SHA1, what I had configured in Firebase was the launch SHA1.

1 answer

0


I was able to solve using the latest version, with Credentials.

If anyone wants to use it that way, here’s the code:

Future<void> verificaPhone() async {
    try {
      final phoneNumber = "+55" +
          _phoneNumberController.text.replaceAll("(", "")
              .replaceAll(")", "")
              .replaceAll("-", "");
      if(_phoneNumberController.text.length > 13) {

        final PhoneVerificationCompleted verificationCompleted =
            (AuthCredential phoneAuthCredential) {
          _auth.signInWithCredential(phoneAuthCredential);

          Navigator.of(context).pushReplacement(
              MaterialPageRoute(builder: (context) => ConfereCadastro()));
          print('Auto login realizado: user');
//          setState(() {
//            Navigator.of(context).pushReplacement(
//                MaterialPageRoute(builder: (context) => ConfereCadastro()));
//            print('Auto login realizado: user');
//          });
        };

        final PhoneVerificationFailed verificationFailed = (
            AuthException authException) {
          print('Verificação para o número ${phoneNumber
              .toString()} falhou. Código: ${authException
              .code}. Motivo: ${authException.message}');
//          setState(() {
//            print('Verificação para o número ${phoneNumber
//                .toString()} falhou. Código: ${authException
//                .code}. Motivo: ${authException.message}');
//          });
        };

        final PhoneCodeSent codeSent =
            (String verificationId, [int forceResendingToken]) async {
          this.verificationId = verificationId;
          print("Código enviado para " + phoneNumber);
          setState(() => codigoEnviado = true);
        };

        final PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout =
            (String verificationId) {
          this.verificationId = verificationId;
          setState(() => codigoEnviado = false);
          print("Tempo limite esgotado!");
        };

        await FirebaseAuth.instance.verifyPhoneNumber(
            phoneNumber: phoneNumber,
            timeout: const Duration(seconds: 30),
            verificationCompleted: verificationCompleted,
            verificationFailed: verificationFailed,
            codeSent: codeSent,
            codeAutoRetrievalTimeout: codeAutoRetrievalTimeout);
      }
    } catch (e) {
      print("_signInWithPhoneNumber ERROR: ${e.toString()}");
    }

  }

  Future<void> _signInWithPhoneNumber(String smsCode) async {
    if(hasConnection == true) {
      try {
        AuthCredential credential = PhoneAuthProvider.getCredential(verificationId: verificationId, smsCode: smsCode);

        final FirebaseUser user = await _auth.signInWithCredential(credential);
        print('Usuário logado com sucesso $user');
        Navigator.of(context).pushReplacement(
            MaterialPageRoute(builder: (context) => ConfereCadastro()));
      } catch (e) {
        print("verificaPhone ERROR: ${e.toString()}");
      }
    } else {
      checkInternet();
    }
  }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.