0
votes

I build react native app with firebase phone auth after I generated signed apk in android by

cd android && ./gradlew assembleRelease

I tested on my phone with the apk I generated and when I enter my phone nothing happened and it return my error

Error: this app is not authorized to use firebase authentication. please verifythat the correct package name and sha-1 are configured in the firebase console.

BUT when I run the app without the apk, with

react-native run-android

and the authentication works good and I register user.

 confirmPhone = async (phoneNumber) => {
        const phoneWithAreaCode = phoneNumber.replace(/^0+/,'+972');
        return new Promise((res, rej) => {
            firebase.auth().verifyPhoneNumber(phoneWithAreaCode)
                .on('state_changed', async (phoneAuthSnapshot) => {
                    console.log('phone-->',phoneAuthSnapshot)
                    switch (phoneAuthSnapshot.state) {
                    case firebase.auth.PhoneAuthState.AUTO_VERIFIED:
                        await this.confirmCode(phoneAuthSnapshot.verificationId, phoneAuthSnapshot.code, phoneAuthSnapshot)
                        res(phoneAuthSnapshot)

                        break

                    case firebase.auth.PhoneAuthState.CODE_SENT:
                        UserStore.setVerificationId(phoneAuthSnapshot.verificationId)
                        res(phoneAuthSnapshot)
                        break

                    case firebase.auth.PhoneAuthState.AUTO_VERIFY_TIMEOUT: 
                        UserStore.setVerificationId(phoneAuthSnapshot.verificationId)
                        UserStore.setErrorCodeAuthentication('SMS code has expired')
                        res(phoneAuthSnapshot)


                    case firebase.auth.PhoneAuthState.ERROR:
                        // console.log(phoneAuthSnapshot)
                        // if(NavigationStore.CurrentRoute == 'Login'){
                        //     UserStore.setErrorCodeAuthentication('Please enter valid phone number')
                        // }else
                        //     UserStore.setErrorCodeAuthentication('Pin code invalid')
                        rej(phoneAuthSnapshot)
                        break

                    }
                })
        })
    }

    confirmCode = async (verificationId, code, phoneAuthSnapshot) => {
           try{
            const credential = await firebase.auth.PhoneAuthProvider.credential(UserStore.verificationId, code)
            UserStore.setCodeInput(code)
            UserStore.setUserCredentials(credential)
            AppStore.setAlreadyRegister(true)
            await this.authenticate(credential)
            return credential
           } catch(e){
               throw new Error(e)
           }


    }

    authenticate = async (credential) => {
         await firebase.auth().signInAndRetrieveDataWithCredential(credential)

    }`
2

2 Answers

3
votes

If you haven't already figured it out, I had the same exact problem.

  1. Open Android Studio
  2. Click the Gradle Taskbar on the right
  3. Double click signingReport from [your app name] > Tasks > android > signingReport
  4. Take note of the Variant: Relase SHA-1 that is generated. It should be among the first outputted by the task.
  5. Append this SHA-1 to the list of SHA-1 in your firebase console. You should have 2 now, one for debug and one for release.
  6. Download the fresh google-services.json and place it in your android/app directory.
  7. Rebuild release app.
1
votes

Go to the file location where your signed apk located.

1.Opem cmd and write

keytool -printcert -jarfile app-release.apk

2.Copy the SHA1 and append to the firebase console.

it worked in my case