Apple Sign-In Integration using AppleJS
This guide walks you through the complete process of integrating Apple Sign-In in a React application using AppleJS.
Prerequisites
- Node.js and npm installed
- React project set up (e.g., via
create-react-appor Next.js) - Apple Developer Account ($99/year)
- Custom domain with HTTPS (required by Apple for redirect URLs)
Step 1: Apple Developer Account Setup
1.1 Create App ID
- Log in to Apple Developer Console.
- Navigate to Certificates, Identifiers & Profiles.
- Go to Identifiers > App IDs > + to register a new App ID.
- Fill out:
- Description: Your app name
- Bundle ID:
com.yourdomain.yourapp
- Under Capabilities, check
Sign In with Apple. - Click Continue > Register.
1.2 Create Service ID
- Go to Identifiers > Services IDs > + to register a new Service ID.
- Fill out:
- Description: Your app name
- Identifier:
com.yourdomain.yourapp.service
- Enable
Sign In with Apple. - Click Configure:
- Add your domain and return URLs (e.g.,
https://yourdomain.com)
- Add your domain and return URLs (e.g.,
- Click Continue > Register.
1.3 Create Key (.p8)
- Navigate to Keys > + to create a new key.
- Fill out:
- Key Name:
Sign In with Apple Key
- Key Name:
- Enable
Sign In with Apple. - Click Configure:
- Select the App ID you created earlier.
- Select the App ID you created earlier.
- Click Continue > Register > Download the
.p8file.
Important: Save this file securely. It can only be downloaded once.
Make note of:
- Key ID
- Team ID (found in your Apple developer account)
Step 2: Implement Apple Sign-In in React
Embed AppleJS Script
In your component or _document.tsx, include:
<Script src="https://appleid.cdn-apple.com/appleauth/static/jsapi/appleid/1/en_US/appleid.auth.js" />
Handle Sign-In Function
function handleAppleSignIn() {
AppleID.auth.init({
clientId: process.env.NEXT_PUBLIC_APPLE_CLIENT_ID,
scope: 'name email',
redirectURI: process.env.NEXT_PUBLIC_AUTH_URL,
state: 'state123',
usePopup: true,
});
AppleID.auth.signIn().then((response: any) => {
if (response?.authorization?.id_token) {
const id_token = response.authorization.id_token;
const decodedToken = jwt.decode(id_token);
const { email, sub } = decodedToken as { email: string; sub: string };
const cookieData : SessionCookie = {
email: email,
idToken: id_token,
sub: sub,
type: "Apple"
};
document.cookie = `session=${encodeURIComponent(JSON.stringify(cookieData))}; path=/; max-age=${60 * 60 * 24 * 7}; SameSite=Strict`;
const sessionCookie = getSessionData();
if (sessionCookie) {
// navigate to a success screen
} else {
// error
}
}
}).catch((error: any) => {
console.error('Error during Apple login:', error);
});
}
Apple Sign-In Button
<SocialButton
icon="/images/apple.png"
alt="apple logo"
onClick={() => handleAppleSignIn()}
/>
Step 3: Test Your Integration
Start your React app:
npm start
- Click the Sign in with Apple button.
- Authenticate using your Apple ID.
- You should be redirected to your configured return URL with an ID token.
- Verify that:
- The token is stored correctly.
- User data is fetched successfully.
Notes
Your return URL must use HTTPS and be registered in the Apple Developer Console.
The clientIdmust exactly match the Service ID identifier.- Sign in with Apple feature doesnβt work with localhost (or 127.0.0.1) and requires a real domain/ or domain name with etc/hosts mapping.
Never expose your .p8file or use it in client-side code.
It is for server-side token validation and refresh handling only.




