From ec933011e64d43eae4d8933542f4dc482041c1be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Bu=C3=9Fmann?= Date: Wed, 30 Oct 2019 16:54:14 +0100 Subject: [PATCH] feat(sign-in-with-apple): Added Sign in with Apple (#3200) * feat(Sign in with Apple): Added Sign in with Apple * docs(Sign in with Apple): Updated interfaces to official ones --- .../plugins/sign-in-with-apple/index.ts | 162 ++++++++++++++++++ 1 file changed, 162 insertions(+) create mode 100644 src/@ionic-native/plugins/sign-in-with-apple/index.ts diff --git a/src/@ionic-native/plugins/sign-in-with-apple/index.ts b/src/@ionic-native/plugins/sign-in-with-apple/index.ts new file mode 100644 index 000000000..d26b8b1fb --- /dev/null +++ b/src/@ionic-native/plugins/sign-in-with-apple/index.ts @@ -0,0 +1,162 @@ +import { Injectable } from '@angular/core'; +import { Plugin, Cordova, IonicNativePlugin } from '@ionic-native/core'; + +/** + * @see https://developer.apple.com/documentation/foundation/nspersonnamecomponents/1412193-phoneticrepresentation + */ +export interface NSPersonNameComponents { + /** + * The portion of a name’s full form of address that precedes the name itself (for example, "Dr.," "Mr.," "Ms.") + */ + namePrefix?: string; + + /** + * Name bestowed upon an individual to differentiate them from other members of a group that share a family name (for example, "Johnathan") + */ + givenName?: string; + + /** + * Secondary name bestowed upon an individual to differentiate them from others that have the same given name (for example, "Maple") + */ + middleName?: string; + + /** + * Name bestowed upon an individual to denote membership in a group or family. (for example, "Appleseed") + */ + familyName?: string; + + /** + * The portion of a name’s full form of address that follows the name itself (for example, "Esq.," "Jr.," "Ph.D.") + */ + nameSuffix?: string; + + /** + * Name substituted for the purposes of familiarity (for example, "Johnny") + */ + nickname?: string; + + /** + * The phonetic representation name components of the receiver + */ + phoneticRepresentation?: NSPersonNameComponents; +} + +/** + * @see https://developer.apple.com/documentation/authenticationservices/asauthorizationappleidcredential + */ +export interface AppleSignInResponse { + /** + * The user’s email address i.e. abc@privaterelay.appleid.com + */ + email?: string; + + /** + * An arbitrary string that your app provided to the request that generated the credential + */ + state?: string; + + /** + * A JSON Web Token (JWT) that securely communicates information about the user to your app + */ + identityToken: string; + + /** + * A short-lived token used by your app for proof of authorization when interacting with the app's server counterpart + */ + authorizationCode: string; + + /** + * The user's name + * @see https://developer.apple.com/documentation/foundation/nspersonnamecomponents?language=objc + */ + fullName?: NSPersonNameComponents; + + /** + * An identifier associated with the authenticated user + */ + user?: string; +} + +/** + * @see https://developer.apple.com/documentation/foundation/nserror + */ +export interface NSError { + /** + * The error code + */ + code?: number; + + /** + * A string containing the localized description of the error + */ + localizedDescription?: string; + + /** + * A string containing the localized explanation of the reason for the error + */ + localizedFailureReason?: string; +} + +export interface AppleSignInErrorResponse extends NSError { + /** + * The error i.e. "ASAUTHORIZATION_ERROR" + */ + error?: string; +} + +/** + * @name Sign In With Apple + * @description + * Sign in with Apple makes it easy for users to sign in to your apps and websites using their Apple ID. + * Instead of filling out forms, verifying email addresses, and choosing new passwords, + * they can use Sign in with Apple to set up an account and start using your app right away. + * All accounts are protected with two-factor authentication for superior security, + * and Apple will not track users’ activity in your app or website. + * *Source:* https://developer.apple.com/sign-in-with-apple/ + * + * @usage + * ```typescript + * import { SignInWithApple, AppleSignInResponse, AppleSignInErrorResponse } from '@ionic-native/sign-in-with-apple/ngx'; + * + * + * constructor(private signInWithApple: SignInWithApple) { } + * + * ... + * + * + * this.signInWithApple.signin() + * .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); + * }); + * + * ``` + */ +@Plugin({ + pluginName: 'Sign in with Apple', + plugin: 'cordova-plugin-sign-in-with-apple', + pluginRef: 'cordova.plugins.SignInWithApple', + repo: 'https://github.com/twogate/cordova-plugin-sign-in-with-apple', + platforms: ['iOS'] +}) +@Injectable() +export class SignInWithApple extends IonicNativePlugin { + + /** + * Starts the authorization flows named during controller initialization + * @see https://developer.apple.com/documentation/authenticationservices/asauthorizationcontroller/3153047-performrequests + * @return {Promise} Returns a promise when authorization succeeds + * @param arg0 + * @throws AppleSignInErrorResponse + */ + @Cordova() + signin(arg0: object = null): Promise { + return; + } + +}