ios – perform sign in with apple in react native

0
87

[ad_1]

I am trying to add sign in with apple to my react native project , I am using managed workflow (using expo-apple-authentication). I am testing on ios 13.7 emulator , the app is running , taking credentials , returning an identity token and other infos about the user .
my question is how to use this token ? As I know, it should be communicated with apple servers but couldnt find any docs related on how to implement this exactly . can anyone help me with this please?
here is my code :

AppleId = async () => {
    try {

       
        const csrf = Math.random().toString(36).substring(2, 15);
        const nonce = Math.random().toString(36).substring(2, 10);
        const hashedNonce = await Crypto.digestStringAsync(
        Crypto.CryptoDigestAlgorithm.SHA256, nonce); //SHA256
      const Credential = await AppleAuthentication.signInAsync({
        requestedScopes: [
            AppleAuthentication.AppleAuthenticationScope.FULL_NAME,
            AppleAuthentication.AppleAuthenticationScope.EMAIL,
        ],
        state: csrf,
        nonce: hashedNonce
    })
           
  
    ;
    const { identityToken } = Credential;
    console.log('idtok',identityToken);
   // console.log('email',email);
   // console.log('state',state);
    console.log('nonce',nonce);
    if (!Credential.identityToken) {
        throw 'Apple Sign-In failed - no identify token returned';
      }
    
      // Create a Firebase credential from the response
     // const { identityToken, nonce } = appleAuthRequestResponse;
      const appleCredential = auth.AppleAuthProvider.credential(identityToken, nonce);
    
      // Sign the user in with the credential
      return auth().signInWithCredential(appleCredential);

   // const loginAvailable = await AppleAuthentication.isAvailableAsync();

  }     
   catch (e) {
      if (e.code === 'ERR_CANCELED') {
        // handle that the user canceled the sign-in flow
        console.log('canceled');
      } else {
        // handle other errors
        console.log(e);
      }
    }
  }

and in the view :

 <AppleAuthentication.AppleAuthenticationButton
  buttonType={AppleAuthentication.AppleAuthenticationButtonType.SIGN_IN}
  buttonStyle={AppleAuthentication.AppleAuthenticationButtonStyle.BLACK}
  cornerRadius={5}
  style={{ width: 200, height: 44 }}
  onPress={this.AppleId}
        />

[ad_2]