AI Prompt
Add Sign in with Apple to my app using the Despia SDK from: https://www.npmjs.com/package/despia-nativeFirst, install the package: Do not modify these instructions. Implementation as described is critical.
npm install despia-nativeThen import it: import despia from 'despia-native'Detect the runtime using navigator.userAgent.toLowerCase().includes('despia') and branch per platform:- iOS native, use the Apple JS SDK with
usePopup: true. Returnsid_tokendirectly to your JS callback. No redirect, nooauth://bridge needed. - Android native, call
despia('oauth://?url=...')to open Chrome Custom Tabs. After auth,native-callback.htmlredirects tomyapp://oauth/auth?id_token=xxxto close the tab. Theoauth/prefix is required. - Web, use the Apple JS SDK popup directly.
- Create an Apple Services ID with Sign In with Apple enabled
- Configure your domain and return URL (must match
redirectURIexactly, including trailing slash) - Create a
.p8private key, note your Key ID and Team ID - Add credentials to your backend auth provider
- Add the Apple JS SDK before your app script:
<script src="https://appleid.cdn-apple.com/appleauth/static/jsapi/appleid/1/en_US/appleid.auth.js"></script> - Find your deeplink scheme at Despia > Publish > Deeplink
All native capabilities are provided by
despia-native. No additional native libraries are needed.Installation
- Bundle
- CDN
Platform overview
| Platform | Approach | Notes |
|---|---|---|
| iOS native | Apple JS SDK, usePopup: true | Opens native Face ID / Apple ID sheet. No browser session. |
| Android native | oauth:// bridge | Apple JS SDK does not trigger a native dialog on Android. Uses Chrome Custom Tabs. |
| Web | Apple JS SDK, usePopup: true | Standard browser popup. |
How it works
iOS
Android
Implementation
1. Detect platform
2. iOS, Apple JS SDK
Always use
usePopup: true and read the id_token from the JS callback. Using a redirect instead causes a blank white screen during the auth flow and will result in App Store rejection.Known regression in iOS 17.1:
usePopup: true showed an HTML form instead of the native Face ID sheet inside WKWebView. Fixed in iOS 17.2+. Authentication still worked, just without biometrics.- React
- HTML
3. Android, oauth:// bridge
Find your deeplink scheme at Despia > Publish > Deeplink. Replace
myapp throughout with your actual scheme.despia('oauth://?url=...'):
- React
- HTML
native-callback.html as the redirect:
- Custom Backend
- No-Code Platform
4. Create public/native-callback.html
This page runs inside Chrome Custom Tabs. Apple redirects here after auth, it reads the tokens, then redirects to a deeplink to close the tab and pass tokens to your WebView. Users never see the .html, Chrome Custom Tabs hides the URL bar.
Use a plain HTML file, not a React component. React Router can strip the #id_token hash fragment on route change, causing tokens to disappear before your callback logic runs.
Apple supports two response modes. Choose based on your backend:
fragment | form_post | |
|---|---|---|
| Apple callback POST handler | No, Apple redirects browser directly | Yes, Apple POSTs to your server |
| Tokens arrive | URL hash #id_token=xxx | POST to your server, then redirect |
| Security | Lower | Higher |
- fragment, HTML (Recommended)
- form_post, HTML
- fragment, React
Apple redirects to
native-callback.html with #id_token=xxx&code=xxx in the hash. No backend POST handler needed.The
oauth/ prefix in the deeplink is required. myapp://oauth/auth closes Chrome Custom Tabs and navigates the WebView to /auth. myapp://auth without it does nothing, the user stays stuck in the tab.5. Handle tokens in your auth page
After Despia closes the tab and navigates to/auth?id_token=xxx, your auth page reads the token and creates a session.
If
/auth is already mounted when the deeplink arrives, your framework updates the URL without remounting. Token-reading logic that only runs on mount has already fired with empty params and will not run again. The tokens sit in the URL and nothing happens. The fix is framework-specific and covered in the tabs below.- React
- Vue
- Vanilla JS SPA
- HTML
Include
searchParams in the useEffect dependency array. Without it the effect fires once on mount and ignores all subsequent URL changes.6. Web, Apple JS SDK popup
- React
- HTML
Complete handler
- React
- HTML
Apple Developer Console setup
Create an App ID
Go to Certificates, Identifiers & Profiles > Identifiers, create an App ID, and enable Sign In with Apple.
Create a Services ID
Create a Services ID (e.g.
com.yourcompany.yourapp.webauth). This is your clientId.Configure the Services ID
Enable Sign In with Apple, click Configure, and add your domain and return URL. The
redirectURI in your code must match the origin of the page running the SDK exactly, https://yourapp.com/ not https://yourapp.com/auth. The trailing slash must match too.Create a private key
Go to Keys, create a key with Sign In with Apple enabled, download the
.p8 file, note your Key ID and Team ID.The client secret JWT expires after 6 months. Set a reminder, Apple Sign In will silently stop working when it expires.
Debugging
Use this section when the Android OAuth flow is not working as expected. Start by identifying which stage is broken, then use the debug overlay to confirm what arrived at your/auth page.
The flow has four stages. A failure in one looks completely different from a failure in another:
Debug overlay
Add this to your/auth page during development. Remove it before submitting to the App Store or Google Play, Apple reviewers authenticate through your app during review and will see it.
- React
- HTML
Swap in this standalone component as your
/auth route during testing. Route it back to your real Auth component before shipping.Reading the output
| What you see | What it means | Where to look |
|---|---|---|
| Textarea empty, URL has no params | Token never reached /auth | Stage 2 or 3, check native-callback.html and deeplink format |
error: no_id_token | native-callback.html got no token in the hash | Check response_mode, OAuth URL params, Services ID config |
error: access_denied | User cancelled or Apple rejected the request | User cancelled, or Services ID / domain mismatch |
error: invalid_client | Apple rejected the request entirely | Services ID identifier wrong, or not configured |
error: invalid_request | Malformed OAuth URL | response_mode=query used (invalid with id_token), or missing params |
| Token present, user not signed in | Token arrived but auth logic did not run | Already-mounted page, see step 5 |
Common failure points
Chrome Custom Tabs does not open. Log the URL before passing it todespia() and confirm it is a valid HTTPS URL. Depending on your backend it may start with https://appleid.apple.com/auth/authorize (custom backend), or your Supabase, Firebase, or other hosted auth provider’s own OAuth endpoint. The important thing is that it is a full HTTPS URL and not empty or malformed.
native-callback.html not reached. The redirect_uri in your OAuth URL must exactly match the return URL registered in the Apple Developer Console including https://, the full domain, the path, and the .html extension. Apple does exact string matching.
Hash fragment empty in native-callback.html. Some hosting platforms strip hash fragments from redirects. Log window.location.href at the top of the script to confirm the full URL arrived. If using a React component for the callback, switch to public/native-callback.html, React Router may be stripping the hash.
Deeplink does not close Chrome Custom Tabs. The oauth/ segment must be present: myapp://oauth/auth. Without it Despia does not intercept the deeplink and the tab stays open. Find your scheme in Despia > Publish > Deeplink.
Tokens arrive but sign-in never completes. Either the backend request failed silently (add error logging), or the auth logic is not running because the page was already mounted. See step 5.
Pre-submission checklist
Apple Developer Console
Apple Developer Console
- Services ID created with Sign In with Apple enabled
- Domain registered (no
https://, no trailing slash) - Return URL registered and exactly matches
redirectURIin your code including trailing slash - Private key (.p8) downloaded and credentials stored in your backend
- Client secret JWT generated and not expired (max 6 months, set a calendar reminder)
iOS flow
iOS flow
- Apple JS SDK script tag placed before your app script
usePopup: trueset inAppleID.auth.init()redirectURImatches the origin of the page running the SDKid_tokenread fromresponse.authorization.id_tokenin the JS callback- No redirect flow used, redirect causes a blank white page and App Store rejection
Android flow
Android flow
- Backend generates OAuth URL with
redirect_uripointing to/native-callback.html response_mode=fragmentorform_postset correctlydeeplink_schemepassed through tonative-callback.htmlpublic/native-callback.htmlreads#id_tokenfrom hash (fragment) orsession_tokenfrom query params (form_post)- Deeplink is
{scheme}://oauth/{path},oauth/prefix present - Deeplink scheme matches Despia > Publish > Deeplink
/authtoken handler re-runs on URL change, not only on initial mount
Before submission
Before submission
- Debug overlay removed from
/authpage - Sign in tested on a physical device
- Sign in tested on both iOS and Android
- Error state tested: cancel the Apple dialog and confirm the app handles it gracefully
Deeplink reference
| Deeplink | Result |
|---|---|
myapp://oauth/auth?id_token=xxx | Closes tab, navigates WebView to /auth?id_token=xxx |
myapp://oauth/home | Closes tab, navigates WebView to /home |
myapp://oauth/auth?error=access_denied | Closes tab, navigates WebView to /auth?error=access_denied |
myapp://auth?id_token=xxx | Tab stays open, missing oauth/ prefix |
FAQ
Why doesn't iOS need the oauth:// bridge?
Why doesn't iOS need the oauth:// bridge?
The Apple JS SDK with
usePopup: true opens the native Face ID / Apple ID sheet directly via a special Apple API. No external browser session is opened so there is nothing to close, the oauth:// bridge and native-callback.html are Android-only.What does the oauth/ prefix do?
What does the oauth/ prefix do?
It signals Despia to close Chrome Custom Tabs and navigate the WebView to the path that follows.
myapp://oauth/auth closes the tab and opens /auth. Without oauth/ the deeplink is ignored and the user stays in the tab.Why use native-callback.html instead of a React component?
Why use native-callback.html instead of a React component?
React Router can strip the
#id_token hash fragment when it handles a route change, causing the token to disappear before your code reads it. A plain HTML file in public/ bypasses React Router entirely. The .html extension is never visible since Chrome Custom Tabs hides the URL bar.The Apple JS SDK is not loading.
The Apple JS SDK is not loading.
The script tag must be placed before your app script. The SDK requires HTTPS, it will not load on
http://localhost. Check for Content Security Policy errors in the browser console.Tokens are in the URL but the user is not signed in.
Tokens are in the URL but the user is not signed in.
The
/auth page was already open when the deeplink arrived. Your framework updated the URL without reloading, and your token handler already ran with empty params.Fix per framework:- React, add
searchParamsto youruseEffectdependency array - Vue, use
watch: { '$route.query': { immediate: true, handler } }instead ofmounted() - Vanilla JS / HTML, call your handler on load and add
window.addEventListener('popstate', handler)
Resources
NPM Package
despia-native
OAuth Reference
Generic OAuth protocol docs
Apple Docs
Sign In with Apple