All native capabilities here come from
despia-native. No additional native libraries are needed. The Apple JS SDK script tag must be placed before your app script.Installation
- Bundle
- CDN
Platform overview
How it works
On iOS, the SDK has a special hook into the system that lets it summon the native Apple ID sheet from inside a WebView, so no external browser is involved. On Android no such hook exists, so the flow opens Chrome Custom Tabs, lets Apple redirect to a small bridge HTML file, and uses a deeplink with theoauth/ prefix to close the tab and pass the token back to the WebView.
iOS
Android
Platform detection
Use the canonical user agent check. All three constants are derived from the sametoLowerCase() call. Gate every native call behind the relevant constant so the flow degrades cleanly in a browser and on the wrong platform.
iOS, Apple JS SDK popup
Initialize the SDK and callsignIn() with usePopup: true. The id_token arrives directly in the JS callback, no redirect, no oauth:// bridge.
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 and later. Authentication still worked, just without biometrics.- React
- HTML
Android, oauth:// bridge
Your frontend asks your backend for an Apple OAuth URL, then opens it viadespia('oauth://?url=...').
Find your deeplink scheme at Despia > Publish > Deeplink. Replace
myapp throughout with your actual scheme.Frontend trigger
- React
- HTML
Backend OAuth URL endpoint
Generates the Apple OAuth URL withnative-callback.html as the redirect. Pass the deeplink scheme through as a query param so the bridge page can read it.
- Custom Backend
- No-Code Platform
The native-callback.html bridge
This page runs inside Chrome Custom Tabs. Apple redirects here after auth, the page reads the tokens, then fires a deeplink to close the tab and pass tokens back to the WebView. Users never see the.html because Chrome Custom Tabs hides the URL bar.
Use a plain HTML file in public/, 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. Pick one:
- 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.Handling tokens at /auth
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.Web, Apple JS SDK popup
- React
- HTML
Complete cross-platform handler
- React
- HTML
Apple Developer Console setup
1
Create an App ID
Go to Certificates, Identifiers & Profiles > Identifiers, create an App ID, and enable Sign In with Apple.
2
Create a Services ID
Create a Services ID (e.g.
com.yourcompany.yourapp.webauth). This is your clientId.3
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.4
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.5
Configure your backend
- Custom Backend
- No-Code Platform
Generate a signed JWT from your
.p8 key to use as client_secret. Use jsonwebtoken (Node.js) or equivalent. Valid for up to 6 months.The client secret JWT expires after 6 months. Set a calendar 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. Identify which stage is broken first, 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
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 Handling tokens at /auth above.
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
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