nhosoya / omniauth-apple

OmniAuth strategy for Sign In with Apple
MIT License
261 stars 99 forks source link

can somebody show me the way to integrate this in rails api while using react native. I am done with the mobile end that sends the authorization_code and email but confused at backend part. #18

Closed kamalpandey closed 1 year ago

skotchpine commented 3 years ago

I have done this a few times. The documentation here is lacking

skotchpine commented 3 years ago

@kamalpandey I plan on having this working tonight, and I'm forking to write docs now. I'll keep you posted.

Quick overview:

0) devise + doorkeeper + oidc + pkce 1) react-native-app-auth 2) fetch + Authorization header 3) omniauth apple setup 4) profit

jackspiva commented 3 years ago

@skotchpine any luck?

skotchpine commented 3 years ago

Oh yeah, no problem!

skotchpine commented 3 years ago

I know this doesn't answer MOST of this issue, but I think it answers covers the scope of this library #62

erkie commented 3 years ago

@skotchpine's guide does an excellent job of how to setup the library. I just wanted to expand on the final part, how to sync in-app credentials with the omniauth backend.

You need to pass some variables back to the omniauth callback endpoint.

const appleAuthRequestResponse = await appleAuth.performRequest({
  requestedOperation: appleAuth.Operation.LOGIN,
  requestedScopes: [appleAuth.Scope.EMAIL],
});

// get current authentication state for user
// /!\ This method must be tested on a real device. On the iOS simulator it always throws an error.
const credentialState = await appleAuth.getCredentialStateForUser(appleAuthRequestResponse.user);

// use credentialState response to ensure the user is authenticated
if (credentialState === appleAuth.State.AUTHORIZED) {
  // HERE:
  const serverResponse = await apiClient.perform("GET", "https://yourserver/auth/apple/callback", {
    type: "json",
    queryParams: {
      code: appleAuthRequestResponse.authorizationCode,
      state: appleAuthRequestResponse.state,
      id_token: appleAuthRequestResponse.identityToken,
      nonce: appleAuthRequestResponse.nonce,
      user: JSON.stringify({
        email: appleAuthRequestResponse.email
      })
    }
  });

  if (serverResponse.success) {
    alert("Done")
  }
}

Replace apiClient.perform with your preferred way of sending requests to your server (fetch for example).

skotchpine commented 3 years ago

I'd also like to add that I have been very pleased with this library: https://github.com/FormidableLabs/react-native-app-auth

I think this issue is really out of scope for this library. If you'd like more advice, I'd be glad to do a call. My email is: tycinnamon@gmail.com

To be clear: I'd recommend closing the issue.