> ## Documentation Index
> Fetch the complete documentation index at: https://auth0.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Add Login to Your Ionic Vue with Capacitor Application

> This guide demonstrates how to integrate Auth0 with an Ionic Vue & Capacitor application using the Auth0 Vue SDK.

> [accordion] Use AI to integrate Auth0
>
> If you use an AI coding assistant like Claude Code, Cursor, or GitHub Copilot, you can add Auth0 authentication automatically in minutes using [agent skills](https://agentskills.io/home).
> First, install the Auth0 agent skills:
>
> ```bash theme={null}
> npx skills add auth0/agent-skills --skill auth0
> ```
>
> Then, ask your AI assistant:
>
> ```text theme={null}
> Add Auth0 authentication to my Ionic Vue app
> ```
>
> Your AI assistant will automatically create your Auth0 application, fetch credentials, install the Auth0 Vue SDK and Capacitor plugins, configure deep linking, and implement login/logout flows with native browser integration. Learn more about [Auth0 agent skills](https://auth0.com/docs/quickstart/agent-skills.md).

## Get Started

This quickstart demonstrates how to add Auth0 authentication to an Ionic Vue application running on iOS and Android with Capacitor. You'll build a secure mobile app with login, logout, and user profile features using the Auth0 Vue SDK and Capacitor's native browser plugins.

**1. Create a new project**

Create a new Ionic Vue project with Capacitor

```shellscript theme={null}
npx ionic start auth0-ionic-vue blank --type=vue --capacitor
```

Open the project

```shellscript theme={null}
cd auth0-ionic-vue
```

> [!WARNING]
>
> Make sure you're using the `@ionic/cli` package (not the deprecated `ionic` package). If you see errors about `--npm-client` during project creation, update your CLI:
>
> ```shellscript theme={null}
> npm uninstall -g ionic
> npm i -g @ionic/cli
> ```

**2. Install the Auth0 Vue SDK and Capacitor plugins**

```shellscript theme={null}
npm install @auth0/auth0-vue @capacitor/browser @capacitor/app
```

> [!NOTE]
>
> **[`@capacitor/browser`](https://capacitorjs.com/docs/apis/browser)** opens the Auth0 Universal Login page in the device's system browser (SFSafariViewController on iOS, Chrome Custom Tabs on Android) for secure authentication.
>
> **[`@capacitor/app`](https://capacitorjs.com/docs/apis/app)** listens for deep link events so your app can handle the OAuth callback when Auth0 redirects back after login.

**3. Setup your Auth0 App**

Next up, you need to create a new app on your Auth0 tenant. Ionic Capacitor apps use the **Native** application type with custom URL scheme callbacks.

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:

> [tab] Quick Setup (recommended)
>
> Create an Auth0 **Native** application and get your credentials.
>
> After creating your app, note the **Domain** and **Client ID** values for the next step.
>
> > [!NOTE]
> >
> > You'll also need to configure callback and logout URLs in the Dashboard. See the URL configuration below.

> [tab] CLI
>
> Run the following commands in your project's root directory to create an Auth0 app:
>
> ```shellscript Mac theme={null}
> # Install Auth0 CLI (if not already installed)
> brew tap auth0/auth0-cli && brew install auth0
>
> # Set up Auth0 app and generate .env file
> auth0 qs setup --app --type native --framework ionic-vue --build-tool vite --name "My Ionic Vue App"
> ```
>
> ```powershell Windows theme={null}
> # Install Auth0 CLI (if not already installed)
> scoop bucket add auth0 https://github.com/auth0/scoop-auth0-cli.git
> scoop install auth0
>
> # Set up Auth0 app and generate .env file
> auth0 qs setup --app --type native --framework ionic-vue --build-tool vite --name "My Ionic Vue App"
> ```
>
> > [!NOTE]
> >
> > This command will:
> >
> > 1. Check if you're authenticated (and prompt for login if needed)
> > 2. Create an Auth0 Native application
> > 3. Generate a `.env` file with your Auth0 domain and client ID

> [tab] Dashboard
>
> 1. Head to the [Auth0 Dashboard](https://manage.auth0.com/dashboard/)
>
> 2. Click on **Applications** > **Applications** > **Create Application**
>
> 3. Enter a name for your app (e.g., `Ionic Vue App`), select **Native** as the application type, and click **Create**
>
> 4. Switch to the **Settings** tab and note your **Domain** and **Client ID** values
>
> > [!NOTE]
> >
> > Throughout this guide, `YOUR_PACKAGE_ID` is your application's package ID. This can be found and configured in the `appId` field in your `capacitor.config.ts` file. See [Capacitor's Config schema](https://capacitorjs.com/docs/config#schema) for more info.
>
> Configure the following URLs on the **Settings** tab of your Application Details page:
>
> **Allowed Callback URLs:**
>
> ```text theme={null}
> YOUR_PACKAGE_ID://{yourDomain}/capacitor/YOUR_PACKAGE_ID/callback
> ```
>
> **Allowed Logout URLs:**
>
> ```text theme={null}
> YOUR_PACKAGE_ID://{yourDomain}/capacitor/YOUR_PACKAGE_ID/callback
> ```
>
> **Allowed Origins:**
>
> ```text theme={null}
> capacitor://localhost, http://localhost
> ```
>
> > [!NOTE]
> >
> > **Allowed Callback URLs** are a critical security measure to ensure users are safely returned to your application after authentication. Without a matching URL, the login process will fail, and users will be blocked by an Auth0 error page instead of accessing your app.
> >
> > **Allowed Logout URLs** are essential for providing a seamless user experience upon signing out. Without a matching URL, users will not be redirected back to your application after logout and will instead be left on a generic Auth0 page.
> >
> > **Allowed Origins** must include `capacitor://localhost` (iOS) and `http://localhost` (Android) for native app requests to Auth0.

> [!TIP]
>
> If you used **Quick Setup** or **CLI**, go to your app's **Settings** tab on the [Auth0 Dashboard](https://manage.auth0.com/#/applications) to configure the callback and logout URLs shown in the Dashboard tab above.

**4. Configure the Auth0 Plugin**

```typescript src/main.ts {5,6,8,9,14,15,16,17,18,19,20,21,22,23} lines theme={null}
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import { IonicVue } from '@ionic/vue'
import { createAuth0 } from '@auth0/auth0-vue'
import config from '../capacitor.config'

// Build the callback URL using your app's package ID
const redirect_uri = `${config.appId}://{yourDomain}/capacitor/${config.appId}/callback`

const app = createApp(App)
  .use(IonicVue)
  .use(router)

app.use(
  createAuth0({
    domain: '{yourDomain}',
    clientId: '{yourClientId}',
    useRefreshTokens: true,
    useRefreshTokensFallback: false,
    authorizationParams: {
      redirect_uri,
    },
  })
)

router.isReady().then(() => {
  app.mount('#app')
})
```

> [!NOTE]
>
> * **`useRefreshTokens: true`** — Mobile browsers block third-party cookies, so iframe-based silent auth doesn't work. Refresh tokens call the `/oauth/token` endpoint directly.
> * **`useRefreshTokensFallback: false`** — Prevents the SDK from attempting the iframe-based fallback, which is unavailable on mobile.
> * **`router.isReady().then(...)`** — Ensures Vue Router is fully initialized before the SDK processes the OAuth callback, preventing race conditions.

> [!WARNING]
>
> To persist authentication after closing and reopening the application, you may want to set `cacheLocation` to `localstorage`, but please be aware of [the risks of storing tokens in localstorage](https://auth0.com/docs/libraries/auth0-single-page-app-sdk#change-storage-options). Also, localstorage should be treated as **transient** in Capacitor apps as the data might be recovered unexpectedly. Please read the [guidance on storage in the Capacitor docs](https://capacitorjs.com/docs/guides/storage#why-cant-i-just-use-localstorage-or-indexeddb).

**5. Create Authentication Components**

Create component files

```shellscript Mac/Linux theme={null}
mkdir -p src/components && touch src/components/LoginButton.vue && touch src/components/LogoutButton.vue && touch src/components/UserProfile.vue
```

```powershell Windows theme={null}
New-Item -ItemType Directory -Force -Path src/components
New-Item -ItemType File -Path src/components/LoginButton.vue
New-Item -ItemType File -Path src/components/LogoutButton.vue
New-Item -ItemType File -Path src/components/UserProfile.vue
```

Add the following code to the new components, and update the existing `App.vue` and `HomePage.vue`

**6. Run your app**

Test in the browser first:

```shellscript theme={null}
ionic serve
```

> [!WARNING]
>
> When running in the browser with `ionic serve`, the custom URL scheme redirect won't work because browsers cannot handle custom URL schemes. For browser testing, temporarily change `redirect_uri` to `http://localhost:8100` in `src/main.ts` and add `http://localhost:8100` to your Auth0 app's **Allowed Callback URLs** and **Allowed Logout URLs** in the Dashboard. Remember to revert this change before building for native.

To run on a device or simulator, first add the native platforms:

```shellscript theme={null}
npx cap add ios
npx cap add android
```

Then build, sync, and run:

```shellscript iOS theme={null}
ionic build && npx cap sync && npx cap run ios
```

```shellscript Android theme={null}
ionic build && npx cap sync && npx cap run android
```

> [!NOTE]
>
> You must add native platforms with `npx cap add` before you can run on them. This only needs to be done once per platform. Make sure you've registered the [custom URL scheme](#custom-url-scheme-setup) for your platform first.

> [!TIP]
>
> **Checkpoint**
>
> You should now have a fully functional Auth0 login experience in your Ionic Vue application with login, logout, and user profile information.

***

## Advanced Usage

> [accordion] Custom URL Scheme Setup
>
> Register your app's custom URL scheme so the OS can route the OAuth callback back to your app after authentication.
>
> ### iOS
>
> Register your custom URL scheme in your `ios/App/App/Info.plist`:
>
> ```xml theme={null}
> <key>CFBundleURLTypes</key>
> <array>
>   <dict>
>     <key>CFBundleURLSchemes</key>
>     <array>
>       <string>YOUR_PACKAGE_ID</string>
>     </array>
>   </dict>
> </array>
> ```
>
> Replace `YOUR_PACKAGE_ID` with the `appId` from your `capacitor.config.ts`. To learn more, read [Defining a Custom URL Scheme](https://developer.apple.com/documentation/xcode/defining-a-custom-url-scheme-for-your-app).
>
> ### Android
>
> Add an intent filter to your `android/app/src/main/AndroidManifest.xml` inside the main `<activity>` tag:
>
> ```xml theme={null}
> <intent-filter>
>   <action android:name="android.intent.action.VIEW" />
>   <category android:name="android.intent.category.DEFAULT" />
>   <category android:name="android.intent.category.BROWSABLE" />
>   <data android:scheme="YOUR_PACKAGE_ID" />
> </intent-filter>
> ```
>
> To learn more, read [Create Deep Links to App Content](https://developer.android.com/training/app-links/deep-linking).
>
> > [!TIP]
> >
> > After modifying native project files, run `npx cap sync` to ensure your changes are applied.

> [accordion] Protecting Routes with Vue Router
>
> Use the built-in `authGuard` from the Auth0 Vue SDK to protect routes that require authentication:
>
> ```typescript src/router/index.ts theme={null}
> import { createRouter, createWebHistory } from '@ionic/vue-router'
> import { authGuard } from '@auth0/auth0-vue'
> import HomePage from '../views/HomePage.vue'
> import ProfilePage from '../views/ProfilePage.vue'
>
> const routes = [
>   {
>     path: '/',
>     name: 'Home',
>     component: HomePage,
>   },
>   {
>     path: '/profile',
>     name: 'Profile',
>     component: ProfilePage,
>     beforeEnter: authGuard,
>   },
> ]
>
> const router = createRouter({
>   history: createWebHistory(import.meta.env.BASE_URL),
>   routes,
> })
>
> export default router
> ```
>
> The `authGuard` automatically redirects unauthenticated users to the Auth0 Universal Login page. After logging in, they are returned to the originally requested route.

> [accordion] Calling Protected APIs
>
> Configure the `audience` parameter in your `createAuth0` configuration to request access tokens for your API:
>
> ```typescript src/main.ts theme={null}
> app.use(
>   createAuth0({
>     domain: '{yourDomain}',
>     clientId: '{yourClientId}',
>     useRefreshTokens: true,
>     useRefreshTokensFallback: false,
>     authorizationParams: {
>       redirect_uri,
>       audience: 'YOUR_API_IDENTIFIER',
>     },
>   })
> )
> ```
>
> Then use `getAccessTokenSilently` in your components to retrieve tokens and make authenticated API calls:
>
> ```vue src/components/ApiCall.vue theme={null}
> <template>
>   <ion-button @click="callApi" :disabled="loading">
>     {{ loading ? 'Calling...' : 'Call Protected API' }}
>   </ion-button>
>   <pre v-if="response">{{ JSON.stringify(response, null, 2) }}</pre>
> </template>
>
> <script setup lang="ts">
> import { ref } from 'vue'
> import { useAuth0 } from '@auth0/auth0-vue'
> import { IonButton } from '@ionic/vue'
>
> const { getAccessTokenSilently } = useAuth0()
> const response = ref(null)
> const loading = ref(false)
>
> const callApi = async () => {
>   try {
>     loading.value = true
>     const token = await getAccessTokenSilently()
>
>     const res = await fetch('https://your-api.com/endpoint', {
>       headers: {
>         Authorization: `Bearer ${token}`,
>       },
>     })
>
>     response.value = await res.json()
>   } catch (error) {
>     console.error('API call failed:', error)
>   } finally {
>     loading.value = false
>   }
> }
> </script>
> ```
>
> > [!TIP]
> >
> > The `getAccessTokenSilently` method retrieves the token from the cache or automatically refreshes it using the refresh token when needed.

> [accordion] Using defineComponent Pattern
>
> If you prefer the explicit `defineComponent` pattern over `<script setup>`, here's how the LoginButton component looks using that approach:
>
> ```vue src/components/LoginButton.vue theme={null}
> <template>
>   <ion-button @click="login" expand="block">Log in</ion-button>
> </template>
>
> <script lang="ts">
> import { defineComponent } from 'vue'
> import { useAuth0 } from '@auth0/auth0-vue'
> import { Browser } from '@capacitor/browser'
> import { IonButton } from '@ionic/vue'
>
> export default defineComponent({
>   components: { IonButton },
>   setup() {
>     const { loginWithRedirect } = useAuth0()
>
>     const login = async () => {
>       await loginWithRedirect({
>         openUrl: (url: string) =>
>           Browser.open({ url, windowName: '_self' }),
>       })
>     }
>
>     return { login }
>   },
> })
> </script>
> ```
>
> The `defineComponent` pattern requires explicitly registering child components and returning values from the `setup()` function. Both patterns are fully supported by the Auth0 Vue SDK.

> [accordion] Common Issues & Solutions
>
> ### Callback URL mismatch error
>
> **Solution:** Verify that the callback URL in your Auth0 Dashboard matches exactly with the URL constructed in your application. Ensure `YOUR_PACKAGE_ID` matches the `appId` field in your `capacitor.config.ts`.
>
> ### "PKCE not allowed" error
>
> **Fix:**
>
> 1. Go to Auth0 Dashboard > Applications > Your Application
> 2. Change the **Application Type** to **Native**
> 3. Set **Token Endpoint Authentication Method** to `None`
> 4. Save changes and try again
>
> ### SSO not working on iOS
>
> Capacitor's Browser plugin uses `SFSafariViewController`, which does not share cookies with Safari on iOS 11+. If you need SSO, use a compatible plugin that uses [ASWebAuthenticationSession](https://developer.apple.com/documentation/authenticationservices/aswebauthenticationsession).
>
> ### Login works but user stays unauthenticated after app restart
>
> Enable `cacheLocation: 'localstorage'` in the `createAuth0` configuration to persist tokens across app restarts. Be aware of the [security implications](https://auth0.com/docs/libraries/auth0-single-page-app-sdk#change-storage-options). The SDK also supports [custom cache implementations](https://github.com/auth0/auth0-spa-js/blob/master/EXAMPLES.md#creating-a-custom-cache) for more secure storage.
>
> ### Browser doesn't close after login
>
> Ensure you are calling `Browser.close()` inside the `appUrlOpen` event listener in your `App.vue` component. On Android, `Browser.close()` is a no-op, so the browser closes automatically.
>
> ### Login page opens in the device's default browser app
>
> If the login page opens in Safari or Chrome instead of an in-app browser overlay, make sure you're passing the `openUrl` callback to `loginWithRedirect` and `logout`. Without it, the SDK defaults to `window.location.href`, which navigates the entire app away.
