Skip to content

Instantly share code, notes, and snippets.

@thibseisel
Last active February 22, 2022 10:42
Show Gist options
  • Save thibseisel/9cf38eb7a3d2063591d2fb0b24eb4093 to your computer and use it in GitHub Desktop.
Save thibseisel/9cf38eb7a3d2063591d2fb0b24eb4093 to your computer and use it in GitHub Desktop.
Type definition for Sign In with Apple JS SDK.
/**
* Sign in with Apple is the fastest way to onboard new users securely
* and provides two-factor authentication. Using Sign in with Apple JS,
* users can to log into your website with their Apple ID rather than
* creating a new account and password.
*
* Enabling Sign in with Apple for your app begins with registering your app
* in your Apple Developer account. When integrating the API with your app,
* consider button presentation style, notification options, server integration,
* and what kind of user information to request.
*
* @see https://developer.apple.com/documentation/sign_in_with_apple/sign_in_with_apple_js/configuring_your_webpage_for_sign_in_with_apple
*/
declare namespace AppleID {
let auth: AuthI
/**
* The interface used to authenticate a user.
*/
interface AuthI {
/**
* Initialize the authentication object with a configuration object.
*
* Use this method to configure the authentication object.
* Alternatively, you can use meta tags on your website to configure the
* authentication object.
*
* ```html
* <head>
* <meta name="appleid-signin-client-id" content="[CLIENT_ID]">
* <meta name="appleid-signin-scope" content="[SCOPES]">
* <meta name="appleid-signin-redirect-uri" content="[REDIRECT_URI]">
* <meta name="appleid-signin-state" content="[STATE]">
* </head>
* ```
*
* @param config The client configuration object used for initilization.
*/
init(config: ClientConfigI): void
/**
* Sign in using the configuration object.
*
* By default, the {@link signIn} method uses the configuration object
* created during initialization. However, you can override
* the initialization values by passing in a new configuration object.
*
* @param signInConfig The configuration object used to sign in the user.
*/
signIn(signInConfig?: ClientConfigI): Promise<SignInResponseI>
/**
* Renders the button contained in the wrapped tag.
*
* Use this function when the framework you're using requires rerender support.
* Calling this function forces the render of the button found in the wrapper tag.
*/
renderButton(): void
}
/**
* An object that contains a user's authorization information.
*/
interface AuthorizationI {
/**
* A single-use authorization code.
* The code is valid for five minutes.
*/
code: string
/**
* A JSON web token containing the user's identify information.
*/
id_token: string
/**
* The state passed at initialization time.
*/
state?: string
}
/**
* An object that contains a user's configuration information.
*/
interface ClientConfigI {
/**
* The developer's client identifier, as provided by WWDR.
*
* You must obtain a client identifier from WWDR
* before you can use Sign In with Apple.
*/
clientId?: string
/**
* The URI to which the authorization redirects.
*
* The URI provided must redirect to a website under your control.
* Suppply this URI to WWDR to enable this feature.
* The host specified in `appleid-signin-redirect-uri` must include a domain name.
* It can’t be an IP address or `localhost`.
*/
redirectURI?: string
/**
* The amount of user information requested from Apple.
*
* You can request the user's name or email.
* You can also choose to request both, or neither.
* Use space separation when requesting multiple scopes;
* for example `"scope=name email"`.
*/
scope?: string
/**
* The current state of the request.
*
* You create and send this property to Apple, and Apple returns it during authentication.
* Provide information about the state of your app inside of the property,
* such as “Initial user authentication request”. Use this property to
* help authenticate the returned response by comparing Apple’s response to
* the state you sent.
*/
state?: string
/**
* The value that associates a client session and an ID token.
*
* This value mitigates replat attacks and is present only if passed
* during the authorization request.
*/
nonce?: string
/**
* A Boolean that enables showing the flow in a popup.
*/
usePopup?: boolean
}
/**
* An object that contains the user's full name.
*/
interface NameI {
/**
* The user's first name.
*/
firstName: string
/**
* The user's last name.
*/
lastName: string
}
/**
* An object that contains the user's name and email address.
*/
interface UserI {
/**
* The user's email address.
*/
email: string
/**
* The user's full name.
*/
name: NameI
}
/**
* An object that contains the response to a sign-in request.
*/
interface SignInResponseI {
/**
* The user's identification information.
*
* Apple only returns the user object the first time the user authorizes the app.
* Persist this information from your app; subsequent authorization requests won’t contain the user object.
*/
user?: UserI
/**
* The user's authorization information.
*/
authorization: AuthorizationI
}
/**
* An object that contains error information.
*/
interface SignInErrorI {
/**
* The error code.
*
* Currently, the only error code returned is `user_cancelled_authorize`.
* The framework returns this error code when the user clicks the Cancel button.
*/
error: string
}
}
// Add "AppleIDSignIn*"" events to document.addEventListener
interface DocumentEventMap {
AppleIDSignInOnSuccess: CustomEvent<AppleID.SignInResponseI>
AppleIDSignInOnFailure: CustomEvent<AppleID.SignInErrorI>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment