skip to Main Content

I have a react native app that uses Expo (managed, not detached) and that uses Firebase auth to provide Facebook login and email/password login.

I now need to implement “Sign in with Apple” as per Apple’s new rules.

Expo provides a way to do this, and it works, returning the user’s info. But because all users are managed through Firebase auth, I need to take what Apple sends me and pass it to Firebase auth.

The Firebase docs explain how to do this using signInWithCustomToken. But that requires that I create the token. In Node this would be simple, but this app is serverless and I haven’t found a tool that can generate an RS256 token on the client. From the Firebase docs it seems that RS256 is a requirement. I’ve tried using expo-jwt with HS256 and Firebase returns an error that the token is badly formed. But besides using HS256 instead of RS256 I see no other possible problems. The token is encoded and decoded successfully as follows.

const appleJwt = JWT.encode(
    {
      familyName: 'M',
      givenName: 'Greg',
      email: '[email protected]',
      alg: 'HS256',
      iss:
        'https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit',
      sub: serviceAccountEmail,
      aud: serviceAccountEmail,
      iat: Math.floor(Date.now() / 1000),
      exp: Math.floor(Date.now() / 1000) + 3600,
      uid: appleUid
    },
    key,
    {
      algorithm: 'HS256'
    }
  );
  console.log('TCL: loginWithApple -> appleJwt', appleJwt);
  const appleJwtDecoded = JWT.decode(appleJwt, key);
  console.log('TCL: loginWithApple -> appleJwtDecoded', appleJwtDecoded);

It’s only when I try to use it with Firebase auth that it returns an error that the token is badly formatted.

return Firebase.auth().signInWithCustomToken(appleJwt).then(...

Note that the key and the serviceAccountEmail were retrieved from the firebase console.

I’m wondering if perhaps there’s some simpler solution that I’m overlooking. The community is awaiting word from Firebase on if they’ll provide out of the box login with Apple, like they do for other providers, so maybe I just need to be patient. But I’d prefer to find a solution.

A big thanks in advance for any advice.

Update 2019-10-15

I built a simple node server with an API that my app could use to generate the token with RS256, but Firebase still responds that the token is badly formatted when I pass it to signInWithCustomToken. Can’t see what’s wrong with it.

So, since I had the node server built, I just configured the Firebase Admin SDK and used the provided createCustomToken to generate the token. Firebase accepts it now when I pass it to signInWithCustomToken, which was my problem, so this issue is settled for me. After the custom Firebase sign in succeeds the first time, I write all the user data to Firestore. For subsequent sign ins, it just updates the last login date in Firestore. Hopefully Firebase will still provide their own solution soon too, since having a separate node server just for this is not ideal.

2

Answers


  1. The Firebase team is working on implementing this in the official sdk

    https://github.com/firebase/firebase-ios-sdk/issues/3145#issuecomment-510178359

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search