Flutter: Firebase Phone Auth Not Authorized, but Google Sign In Successful

1,074

Update: After trying everything for almost 2 Days, i realized that device that i use to test Firebase Phone is Rooted, and installed with Custom ROM.

When i try firebase phone auth in unrooted and Original ROM Installed, firebase phone auth works beautifully.

Looks like firebase phone auth is not available with rooted and/or custom rom installed device.

This question is answered, thanks guys :)

Share:
1,074
Aldo Suhartono Putra
Author by

Aldo Suhartono Putra

Updated on December 15, 2022

Comments

  • Aldo Suhartono Putra
    Aldo Suhartono Putra over 1 year

    I try to implement various Firebase Auth method in my Flutter app, when i try to implement Firebase Phone Auth (firebase_auth), it says this error:

    This app is not authorized to use Firebase Authentication. Please verify that the correct package name and SHA-1 are configured in the Firebase Console.

    My package name is already configured, when i setup my android app Firebase project, it connects successfully.

    Regarding the SHA-1 key, I already configured my Firebase Console to include both my debug key and my release key, i get debug key from: keytool -list -v -alias androiddebugkey -keystore %USERPROFILE%\.android\debug.keystore also i try with my release key, and build my apk in release version.

    I also re-download google-service.json and running flutter clean to ensure everything is clean.

    I also confirm that i run the application in real physical device, not emulator. But Until this point, i have no luck, still stuck (at least 2 days) in above error.

    The strange thing is, when i try to login using Google Sign-In, which also (AFAIK) require correct SHA-1 information, it is works successfully. But, i have no luck in Firebase Phone Auth.

    Many question and answer only address problem about running Firebase Phone in Emulator, or in unconfigured SHA-1 Firebase Console, or wrong debug/release key, or cleaning the project. But in my case, i haven't found any answer for my problem.

    For reference, this is my Sign In with Phone Number dart code (which i get from firebase_auth/example github repo):

    import 'package:flutter/material.dart';
    import 'package:firebase_auth/firebase_auth.dart';
    
    final FirebaseAuth _auth = FirebaseAuth.instance;
    
    class SignInPage extends StatefulWidget {
      final String title = 'Registration';
      @override
      State<StatefulWidget> createState() => SignInPageState();
    }
    
    class SignInPageState extends State<SignInPage> {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
            actions: <Widget>[
              Builder(builder: (BuildContext context) {
                return FlatButton(
                  child: const Text('Sign out'),
                  textColor: Theme.of(context).buttonColor,
                  onPressed: () async {
                    final FirebaseUser user = await _auth.currentUser();
                    if (user == null) {
                      Scaffold.of(context).showSnackBar(const SnackBar(
                        content: Text('No one has signed in.'),
                      ));
                      return;
                    }
                    _signOut();
                    final String uid = user.uid;
                    Scaffold.of(context).showSnackBar(SnackBar(
                      content: Text(uid + ' has successfully signed out.'),
                    ));
                  },
                );
              })
            ],
          ),
          body: Builder(builder: (BuildContext context) {
            return ListView(
              scrollDirection: Axis.vertical,
              children: <Widget>[
                _PhoneSignInSection(Scaffold.of(context))
              ],
            );
          }),
        );
      }
    
      // Example code for sign out.
      void _signOut() async {
        await _auth.signOut();
      }
    }
    
    class _PhoneSignInSection extends StatefulWidget {
      _PhoneSignInSection(this._scaffold);
    
      final ScaffoldState _scaffold;
      @override
      State<StatefulWidget> createState() => _PhoneSignInSectionState();
    }
    
    class _PhoneSignInSectionState extends State<_PhoneSignInSection> {
      final TextEditingController _phoneNumberController = TextEditingController();
      final TextEditingController _smsController = TextEditingController();
    
      String _message = '';
      String _verificationId;
    
      @override
      Widget build(BuildContext context) {
        return Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Container(
              child: const Text('Test sign in with phone number'),
              padding: const EdgeInsets.all(16),
              alignment: Alignment.center,
            ),
            TextFormField(
              controller: _phoneNumberController,
              decoration: const InputDecoration(
                  labelText: 'Phone number (+x xxx-xxx-xxxx)'),
              validator: (String value) {
                if (value.isEmpty) {
                  return 'Phone number (+x xxx-xxx-xxxx)';
                }
                return null;
              },
            ),
            Container(
              padding: const EdgeInsets.symmetric(vertical: 16.0),
              alignment: Alignment.center,
              child: RaisedButton(
                onPressed: () async {
                  _verifyPhoneNumber();
                },
                child: const Text('Verify phone number'),
              ),
            ),
            TextField(
              controller: _smsController,
              decoration: const InputDecoration(labelText: 'Verification code'),
            ),
            Container(
              padding: const EdgeInsets.symmetric(vertical: 16.0),
              alignment: Alignment.center,
              child: RaisedButton(
                onPressed: () async {
                  _signInWithPhoneNumber();
                },
                child: const Text('Sign in with phone number'),
              ),
            ),
            Container(
              alignment: Alignment.center,
              padding: const EdgeInsets.symmetric(horizontal: 16),
              child: Text(
                _message,
                style: TextStyle(color: Colors.red),
              ),
            )
          ],
        );
      }
    
      // Example code of how to verify phone number
      void _verifyPhoneNumber() async {
        setState(() {
          _message = '';
        });
        final PhoneVerificationCompleted verificationCompleted =
            (AuthCredential phoneAuthCredential) {
          _auth.signInWithCredential(phoneAuthCredential);
          setState(() {
            _message = 'Received phone auth credential: $phoneAuthCredential';
          });
        };
    
        final PhoneVerificationFailed verificationFailed =
            (AuthException authException) {
          setState(() {
            _message =
            'Phone number verification failed. Code: ${authException.code}. Message: ${authException.message}';
          });
        };
    
        final PhoneCodeSent codeSent =
            (String verificationId, [int forceResendingToken]) async {
          widget._scaffold.showSnackBar(const SnackBar(
            content: Text('Please check your phone for the verification code.'),
          ));
          _verificationId = verificationId;
        };
    
        final PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout =
            (String verificationId) {
          _verificationId = verificationId;
        };
    
        await _auth.verifyPhoneNumber(
            phoneNumber: _phoneNumberController.text,
            timeout: const Duration(seconds: 60),
            verificationCompleted: verificationCompleted,
            verificationFailed: verificationFailed,
            codeSent: codeSent,
            codeAutoRetrievalTimeout: codeAutoRetrievalTimeout);
      }
    
      // Example code of how to sign in with phone.
      void _signInWithPhoneNumber() async {
        final AuthCredential credential = PhoneAuthProvider.getCredential(
          verificationId: _verificationId,
          smsCode: _smsController.text,
        );
        final FirebaseUser user =
            (await _auth.signInWithCredential(credential)).user;
        final FirebaseUser currentUser = await _auth.currentUser();
        assert(user.uid == currentUser.uid);
        setState(() {
          if (user != null) {
            _message = 'Successfully signed in, uid: ' + user.uid;
          } else {
            _message = 'Sign in failed';
          }
        });
      }
    }
    

    Thanks for your response, before and after.