invertase / react-native-apple-authentication

A React Native library providing support for Apple Authentication on iOS and Android.
Other
1.41k stars 224 forks source link

Expo #251

Closed nandorojo closed 2 years ago

nandorojo commented 2 years ago

Is there a config plugin for this, similar to expo-authentication-apple? Or should I just use that library? Trying to figure out what is best to work with RNFB. @mikehardy any idea? thank you!

It seems like Expo's is effectively just modifying the Info Plist: https://github.com/expo/expo/blob/sdk-43/packages/expo-apple-authentication/plugin/src/withAppleAuthIOS.ts

So maybe I can just do that.

The other difference seems to be that Expo uses a native button, rather than a pure JS one (which wouldn't work on web)

mikehardy commented 2 years ago

There is no expo plugin here, but we're open to one. In general though if you are using expo, I'd definitely use their library. It's not like the firebase stuff where the Expo versions work but just don't do the native integration (thus making a case for expo plugins in RNFB, and we do have them there)

Sad to say this though, as I'm certain if I'm you, I no longer show up here anymore and I have to make sure web auth works here myself :laughing: - it's the truth though. expo apple plugin should do you right

nandorojo commented 2 years ago

Makes sense! In that case, I think I'm going to add web support to expo-apple-authentication 😈. I'll be using it with RNFB.

The one issue: it doesn't seem like Expo's sign in returns a nonce.

Apologies for my ignorance on what the nonce does, but is that required? This is what I see on the docs:

image

Instead, Expo returns this:

image

Does this response from expo's signIn function look sufficient for usage with RNFB?

Expo does allow a nonce to be provided as an argument to signIn, but it isn't part of the response, the way it is with your lib:

image

Thanks for the help here, I really appreciate the fast responses.

I assume I can just pass a nonce to expo, and then the same one to RNFB in the next step, right? And do I just generate a random uuid() on the fly there?

mikehardy commented 2 years ago

I believe "state" is the same thing, @brentvatne would know for absolute certain but I'm 99.99% sure on it based on the text associated with that string being roughly equivalent to all the nonce-related documentation. May do to post a quick clarifying PR to that doc that explicitly uses the word "nonce"

nonce is just an anti-replay attack protection, "Number Only Used Once". I think you want to use the string that comes back from expo signin in state as the nonce for the firebase auth token.

There are a few strategies to use to generate a nonce, I would definitely follow whatever the Expo docs say htere

nandorojo commented 2 years ago

that looks right. thanks!

dmahajan980 commented 2 years ago

@nandorojo can you please share how you went ahead with generating the nonce?

nandorojo commented 2 years ago

I don't really remember but I'll check when I'm at my computer. You mean for Web?

dmahajan980 commented 2 years ago

Thanks for the quick response! Really appreciate that.

I didn't mean it for any specific platform earlier, though I want to use this on iOS only. What I meant was since expo-apple-authentication doesn't provide any nonce, how are you generating the nonce to be supplied to Firebase AuthProvider after sign-in? Also, are you supplying the same nonce to Expo's signIn function?

nandorojo commented 2 years ago

Here's what I did on iOS:

import * as AppleAuthentication from 'expo-apple-authentication'
import auth from '@react-native-firebase/auth'

const signInWithApple = async () => {
  try {
    const { state, identityToken } = await AppleAuthentication.signInAsync({
      requestedScopes: [
        AppleAuthentication.AppleAuthenticationScope.FULL_NAME,
        AppleAuthentication.AppleAuthenticationScope.EMAIL,
      ],
    })

    const credential = auth.AppleAuthProvider.credential(
      identityToken,
      state || undefined
    )

    return auth().signInWithCredential(credential)
    // signed in
  } catch (e: any) {
    if (e.code === 'ERR_CANCELED') {
      // handle that the user canceled the sign-in flow
    } else {
      // handle other errors
    }
  }
}
dmahajan980 commented 2 years ago

Awesome @nandorojo! I now understand what @mikehardy meant when he said:

I believe "state" is the same thing

Thank you so much Fernando for your help :D

conor909 commented 1 year ago

@nandorojo would you have time to explain how the signInWithApple above integrates with React Native FireBase using Expo?

..big fan of the Solito starter repo 👍

nandorojo commented 1 year ago

Did you try out the code? Notice that it signs in with Apple, and then uses the credential & passes it to RN Firebase.

conor909 commented 1 year ago

@nandorojo sorry I got confused with package names and read it wrong, yes makes sense thank you!

maxencehenneron commented 1 year ago

If anyone wants to use this library with managed expo you can do the following:

edit app.json and set "expo.ios.entitlements" to the following value:

{      
      "entitlements": {
        "com.apple.developer.applesignin": ["Default"],
      }
}
RayanMoarkech commented 11 months ago

Thank you @maxencehenneron . I would use expo package, but it does not offer apple sign in on Android.

Arjan-Zuidema commented 10 months ago

Thank you @maxencehenneron . I would use expo package, but it does not offer apple sign in on Android.

Yes, same. Thanks why I am using this package.

If anyone wants to use this library with managed expo you can do the following:

edit app.json and set "expo.ios.entitlements" to the following value:

{      
      "entitlements": {
        "com.apple.developer.applesignin": ["Default"],
      }
}

This fixed my problem!

kAutoman commented 8 months ago

@Arjan-Zuidema I think "expo.ios.entitlements" is for IOS build environment? If I need to build it for android what should I do? Best regards.

Arjan-Zuidema commented 8 months ago

I didn;t had any errors when using Android, so I can't help you, sorry 🙂

kAutoman commented 8 months ago

@Arjan-Zuidema Could you share me the example of app.json for android-Applesignin setup?

Arjan-Zuidema commented 8 months ago

There is nothing related to Apple Sign-in in my app.json for Android

zdraganov commented 2 months ago

It also works for me only on iOS. When I call the signInAsync method on Android I see the following error:

    const { authorizationCode } = await AppleAuthentication.signInAsync({
      requestedScopes: [
        AppleAuthentication.AppleAuthenticationScope.FULL_NAME,
        AppleAuthentication.AppleAuthenticationScope.EMAIL,
      ],
    });

    return authorizationCode;

And the error I see on Android is this one:

 ERROR  [Error: The method or property expo-apple-authentication.signInAsync is not available on android, are you sure you've linked all the native dependencies properly?]

It works on iOS. @Arjan-Zuidema can you share a snippet that works for you on Android?