Skip to content

Instantly share code, notes, and snippets.

@rossbulat
Created July 19, 2020 03:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rossbulat/ccd8eca3adaff6b02159060fda8cad31 to your computer and use it in GitHub Desktop.
Save rossbulat/ccd8eca3adaff6b02159060fda8cad31 to your computer and use it in GitHub Desktop.
Sign in with Apple Component in React Native
import React from 'react'
import styles from './styles'
import appleAuth, {
AppleButton,
AppleAuthError,
AppleAuthRequestScope,
AppleAuthRequestOperation,
} from '@invertase/react-native-apple-authentication'
import { authFetch } from './Utils'
import { getUniqueId } from 'react-native-device-info'
export const AppleSignIn = (props) => {
const handleSignIn = async (data: any) => {
/* Redux actions, persisting data with AsyncStorage, redirection...*/
}
const onAppleButtonPress = async () => {
try {
// make sign in request and return a response object containing authentication data
const appleAuthRequestResponse = await appleAuth.performRequest({
requestedOperation: AppleAuthRequestOperation.LOGIN,
requestedScopes: [
AppleAuthRequestScope.EMAIL,
AppleAuthRequestScope.FULL_NAME
],
});
// retrieve identityToken from sign in request
const {
identityToken,
} = appleAuthRequestResponse;
// you may also want to send the device's ID to your server to link a device with the account
const deviceId = getUniqueId();
// identityToken generated
if (identityToken) {
// send data to server for verification and sign in
const { ack, response } = await authFetch({
url: 'sign-in-with-apple',
body: {
...appleAuthRequestResponse,
deviceId: deviceId
}
});
if (ack === 'success') {
// successfully verified, handle sign in
await handleSignIn(response);
}
} else {
// no token, failed sign in
}
} catch (error) {
if (error.code === AppleAuthError.CANCELED) {
// user cancelled Apple Sign-in
} else {
// other unknown error
}
}
}
return (
<AppleButton
buttonStyle={AppleButton.Style.WHITE}
buttonType={AppleButton.Type.SIGN_IN}
style={styles.appleButton}
onPress={() => onAppleButtonPress()}
/>
)
}
export default AppleSignIn;
@torretorich
Copy link

Thankyou so much it's done
but i didn't get user name and email in response can you help to solve this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment