3

Please, could you help me? I want to add Sign in with ionic 4 but no documentation I found only this https://www.npmjs.com/package/cordova-plugin-sign-in-with-apple but somehow I couldn't use.

Many Thanks

2 Answers2

5

import { SignInWithApple, AppleSignInResponse, AppleSignInErrorResponse, ASAuthorizationAppleIDRequest } from '@ionic-native/sign-in-with-apple/ngx';


  constructor(private signInWithApple: SignInWithApple) { }


    this.signInWithApple.signin({
      requestedScopes: [
        ASAuthorizationAppleIDRequest.ASAuthorizationScopeFullName,
        ASAuthorizationAppleIDRequest.ASAuthorizationScopeEmail
      ]
    })
    .then((res: AppleSignInResponse) => {
      // https://developer.apple.com/documentation/signinwithapplerestapi/verifying_a_user
      alert('Send token to apple for verification: ' + res.identityToken);
      console.log(res);
    })
    .catch((error: AppleSignInErrorResponse) => {
      alert(error.code + ' ' + error.localizedDescription);
      console.error(error);
    });
wickdninja
  • 949
  • 1
  • 10
  • 15
2

Example if you are also using Firebase

Install native plugin

npm i --save @ionic-native/sign-in-with-apple

In your application

import {
  SignInWithApple,
  AppleSignInResponse,
  AppleSignInErrorResponse,
  ASAuthorizationAppleIDRequest
} from '@ionic-native/sign-in-with-apple';

constructor(private afAuth: AngularFireAuth) {}

async nativeAppleAuth(): Promise<void> {
  try {
    const appleCredential: AppleSignInResponse = await SignInWithApple.signin({
      requestedScopes: [
        ASAuthorizationAppleIDRequest.ASAuthorizationScopeFullName,
        ASAuthorizationAppleIDRequest.ASAuthorizationScopeEmail
      ]
    });
    const credential = new firebase.auth.OAuthProvider('apple.com').credential(
      appleCredential.identityToken
    );
    const response = await this.afAuth.auth.signInWithCredential(credential);
    console.log('Login successful', response);
  } catch (error) {
    console.log(error);
  }
}
wickdninja
  • 949
  • 1
  • 10
  • 15