Documentation Index
Fetch the complete documentation index at: https://auth0-feat-ionic-capacitor-quickstart-modernization.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
Get Started
This quickstart demonstrates how to add Auth0 authentication to an Ionic React application with Capacitor. You’ll build a mobile-ready app with login, logout, and user profile features using the Auth0 React SDK and Capacitor’s native browser integration.Create a new project
Create a new Ionic React app with CapacitorOpen the project
If you already have an Ionic React app, make sure Capacitor is enabled. You can add it with
ionic integrations enable capacitor and then npx cap init.Install the Auth0 React SDK and Capacitor plugins
@auth0/auth0-react: Provides React hooks and components for Auth0 authentication@capacitor/browser: Opens the login page in the device’s system browser (SFSafariViewController on iOS, Chrome Custom Tabs on Android)@capacitor/app: Handles deep link callbacks when Auth0 redirects back to your app
Capacitor’s Browser plugin on iOS uses
SFSafariViewController, which on iOS 11+ does not share cookies with Safari on the device. This means SSO will not work on those devices. If you need SSO, use a compatible plugin that uses ASWebAuthenticationSession.Setup your Auth0 App
Next up, you need to create a new app on your Auth0 tenant and add the environment variables to your project.You have three options to set up your Auth0 app: use the Quick Setup tool (recommended), run a CLI command, or configure manually via the Dashboard.
Throughout this quickstart,
YOUR_PACKAGE_ID is your application’s package ID. This is the appId field in your capacitor.config.ts file (e.g. io.ionic.starter). See Capacitor’s Config schema for more info.- Quick Setup (recommended)
- CLI
- Dashboard
Create an Auth0 App and copy the pre-filled Also ensure the Application Type is set to Native and the Token Endpoint Authentication Method is set to None.
.env file with the right configuration values.After creating the app, go to its Settings in the Auth0 Dashboard and update the Allowed Callback URLs and Allowed Logout URLs to replace YOUR_PACKAGE_ID with your actual package ID and YOUR_AUTH0_DOMAIN with your Auth0 domain:Configure the Auth0Provider
Open
src/main.tsx and wrap the App component with the Auth0Provider. The mobile-specific settings useRefreshTokens and useRefreshTokensFallback are required for Ionic apps on iOS and Android.src/main.tsx
useRefreshTokens: Required for Ionic on Android and iOS. Mobile browsers block third-party cookies, so the SDK uses refresh tokens instead of iframe-based silent authentication.useRefreshTokensFallback: Must befalseto prevent the SDK from attempting iframe-based silent auth, which is not available on mobile.authorizationParams.redirect_uri: Uses your package ID as a custom URL scheme so the OS can route the Auth0 callback back to your app.
Create Login, Logout, Profile, and Callback Handler
Create filesAnd add the following code snippetsThe
openUrl callback in LoginButton and LogoutButton uses Capacitor’s Browser plugin to open the Auth0 login and logout pages in the device’s system browser component, rather than navigating away from the app entirely.The App component listens for the appUrlOpen event, which fires when Auth0 redirects back to your app using the custom URL scheme. It calls handleRedirectCallback to exchange the authorization code for tokens, then closes the browser.By default, the SDK’s
loginWithRedirect method uses window.location.href to navigate to the login page, which opens the device’s default browser application. Setting openUrl to use Browser.open keeps the authentication flow within your app’s context for a better user experience.Run your app
Test in the browser firstTo run on a device or simulator
When running in the browser with
ionic serve, the custom URL scheme redirect won’t work. For browser testing, you can temporarily set redirect_uri to http://localhost:8100 and add it to your Auth0 app’s Allowed Callback URLs and Allowed Logout URLs. For full native testing, use npx cap run ios or npx cap run android.CheckpointYou should now have a fully functional Auth0 login running in your Ionic app. When running on a device, tapping “Log in” opens the Auth0 Universal Login Page in the system browser, and after authenticating, you’re redirected back to your app with the user’s profile displayed.
Advanced Usage
Custom URL Scheme Configuration (iOS & Android)
Custom URL Scheme Configuration (iOS & Android)
For Auth0 callbacks to work on devices, register your package ID as a custom URL scheme on each platform.iOS — add to Android — add an intent filter inside the main Replace
ios/App/App/Info.plist:ios/App/App/Info.plist
<activity> in android/app/src/main/AndroidManifest.xml:android/app/src/main/AndroidManifest.xml
io.ionic.starter with your actual appId from capacitor.config.ts.To learn more, read Defining a Custom URL Scheme for iOS, or Create Deep Links to App Content for Android.
Protecting Routes with Ionic Router
Protecting Routes with Ionic Router
Use Auth0’s authentication state to protect specific routes in your Ionic application:The
src/App.tsx
withAuthenticationRequired HOC automatically redirects unauthenticated users to the Auth0 login page when they try to access a protected route.Calling Protected APIs
Calling Protected APIs
Configure your Auth0Provider to include an API audience and use the Then make authenticated API calls from your components:
getAccessTokenSilently method to get access tokens for your backend:src/main.tsx
src/ApiCall.tsx