Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react native android firebase sign in with apple [Error: The supplied auth credential is malformed, has expired or is not currently supported.]

I am implementing sign in with Apple. I can successfully see the Apple login page. I key in the correct credentials. It should be able to sign in/sign up to the firebase based on the returned value from Apple.

However I am getting this error Error: The supplied auth credential is malformed, has expired or is not currently supported. Something must be wrong at the firebase side? You may refer to the onPressAppleLogin function below on the logic. Many thanks!

What I have done:

In Firebase

  1. Authentication with Sign-in provider Apple enabled
  2. My service id is co.myexampleapp.signinwithapple
  3. My authorization callback is https://my-example-app.firebaseapp.com/__/auth/handler

In developer.apple.com

  1. I created a service id co.myexampleapp.signinwithapple with the service Sign In with Apple enabled
  2. I added my-example-app.firebaseapp.com for the Domain and https://my-example-app.firebaseapp.com/__/auth/handler in the Return URLs

My React Native source code

import { appleAuthAndroid } from '@invertase/react-native-apple-authentication';
import firebase from 'react-native-firebase'

getRandomString = (length: any) => {
    let randomChars =
        'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
    let result = ''
    for (let i = 0; i < length; i++) {
        result += randomChars.charAt(Math.floor(Math.random() * randomChars.length))
    }
    return result
}

onPressAppleLogin = async () => {
    const rawNonce = this.getRandomString(20);
    const state = this.getRandomString(20)

    appleAuthAndroid.configure({
        clientId: 'co.myexampleapp.signinwithapple',
        redirectUri: 'https://my-example-app.firebaseapp.com/__/auth/handler',
        responseType: appleAuthAndroid.ResponseType.ALL,
        scope: appleAuthAndroid.Scope.ALL,
        nonce: rawNonce,
        state,
    });

    const response = await appleAuthAndroid.signIn();

    const appleCredential = await firebase.auth.AppleAuthProvider.credential(response.id_token, rawNonce)

    const appleUserCredential = await firebase.auth().signInWithCredential(appleCredential) // error happens here!
}
like image 541
4 Leave Cover Avatar asked Jul 07 '21 15:07

4 Leave Cover


3 Answers

This is 100% due to the wrong Services ID on the server or the client.

I was working on a project in which we have Django as the backend server the backend developer used a different Services ID on the server & I on the client-side used a different Services ID.

How we solved this issue.

Open the Firebase console in the general settings check the bundle ID of the ios app compare it with the bundle ID in your Xcode. Make sure the Services is ID is correct and you have the latest provisioning profile with the Services Id added inside it.

Read this article to understand how to create a service ID. https://firebase.google.com/docs/auth/android/apple?authuser=4

You need to add the same services ID that you created above in the firebase console where you enable apple auth service in the authentication section

In your case you need to add service id

co.myexampleapp.signinwithapple

in the input box that is shown in the screenshot.

enter image description here

like image 186
Rajendran Nadar Avatar answered Oct 26 '22 13:10

Rajendran Nadar


There is some sort of error in initializing the credentials.

Three types of errors may occur:

  1. In the response, the token may be get expired. In that time you can use refresh token function to get new token.
  2. Have a look at the rules in the firebase, if you initialized your app in the locked mode read and write will be set to false. If it so, change it to true.
  3. Check whether you have enabled Api keys.

Important Check whether you enabled the third party access for the Apple Id.

like image 30
Mohamed Irfan Avatar answered Oct 26 '22 12:10

Mohamed Irfan


Since the problem is with token, I suggest you to check the following.

  1. Make sure you provided your email in support email in firebase project settings.
  2. Try logging out before performing signing in operation. Due to improper logout during development this may happen. Make sure you always logout before signin. Helped me in some cases.
  3. Device time - since the token generated will be based on timestamp.
like image 1
Swaminathan V Avatar answered Oct 26 '22 12:10

Swaminathan V