-
-
Save rossbulat/ccd8eca3adaff6b02159060fda8cad31 to your computer and use it in GitHub Desktop.
Sign in with Apple Component in React Native
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thankyou so much it's done
but i didn't get user name and email in response can you help to solve this issue.