Capacitor plugin to support Sign in With Apple
Maintainer | GitHub | Social | Sponsoring Company |
---|---|---|---|
Max Lynch | mlynch | @maxlynch | Ionic |
Maintenance Status: Partially Maintained (help wanted)
To use npm
npm install @capacitor-community/apple-sign-in
To use yarn
yarn add @capacitor-community/apple-sign-in
Sync native files
npx cap sync
import { Plugins } from "@capacitor/core";
import { ResponseSignInWithApplePlugin } from "@capacitor-community/apple-sign-in";
const { SignInWithApple } = Plugins;
try {
const response: ResponseSignInWithApplePlugin = await SignInWithApple.authorize();
} catch (e) {
console.error(e);
}
- Initialize the plugin
import {
SignInResponse,
SignInError,
} from "@capacitor-community/apple-sign-in";
const { Device } = Plugins;
let device = await Device.getInfo();
if (device.platform === "web") {
// Configure and initialize the plugin with correct values
SignInWithApple.init({
clientId: "[CLIENT_ID]",
scope: "[SCOPES]", // scope=name email
redirectURI: "[REDIRECT_URI]",
state: "[STATE]",
usePopup: true, //or false defaults to false
});
}
- Call the plugin when need to authorize user. You can either...
- Request to Sign in programically
SignInWithApple.authorize() .then((response: SignInResponse) => { console.log(response); }) .catch((error: SignInError) => { console.error(error); });
- Or, use the default apple button style by adding the HTML below to your desired location of where the login button would appear
<div id="appleid-signin" data-color="black" data-border="true" data-type="sign in" ></div>
Authorization callback listeners can be configured by adding AppleIDSignInOnSuccess and AppleIDSignInOnFailure
//Listen for authorization success
document.addEventListener("AppleIDSignInOnSuccess", (data) => {
console.log(data);
});
//Listen for authorization failures
document.addEventListener("AppleIDSignInOnFailure", (error) => {
console.error(error);
});
Thanks goes to these wonderful people (emoji key):
Masahiko Sakakibara ๐ป |
This project follows the all-contributors specification. Contributions of any kind welcome!