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

# Add Login to Your Vue Application

> This guide demonstrates how to integrate Auth0, add authentication, and display user profile information in any Vue 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).
>
> **Install:**
>
> ```bash theme={null}
> npx skills add auth0/agent-skills --skill auth0
> ```
>
> **Then ask your AI assistant:**
>
> ```text theme={null}
> Add Auth0 authentication to my Vue app
> ```
>
> Your AI assistant will automatically create your Auth0 application, fetch credentials, install `@auth0/auth0-vue`, create the authentication plugin, and configure your routes. [Full agent skills documentation →](/docs/docs/quickstart/agent-skills)

> [!NOTE]
>
> **Prerequisites:** Before you begin, ensure you have the following installed:
>
> * **[Node.js](https://nodejs.org/en/download)** 20 LTS or newer
> * **[npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm)** 10+ or **[yarn](https://classic.yarnpkg.com/lang/en/docs/install/)** 1.22+ or **[pnpm](https://pnpm.io/installation)** 8+
>
> Verify installation: `node --version && npm --version`
>
> **Vue Version Compatibility:** This quickstart works with **Vue 3.x** and newer.

## Get Started

This quickstart demonstrates how to integrate Auth0 authentication into a Vue.js 3 application. You'll build a responsive single-page app with secure user authentication using Vue's composition API and the Auth0 Vue SDK.

**1. Create a new project**

Create a new Vue 3 project for this Quickstart

```shellscript theme={null}
npm create vue@latest auth0-vue -- --typescript --router --pinia
```

Open the project

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

**2. Install the Auth0 Vue SDK**

```shellscript theme={null}
npm add @auth0/auth0-vue && npm install
```

**3. 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:

> [tab] Quick Setup (recommended)
>
> Create an Auth0 App and copy the pre-filled `.env` file with the right configuration values.

> [tab] CLI
>
> Run the following command in your project's root directory to create an Auth0 app and generate a `.env` file:
>
> ```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 spa --framework vue --build-tool vite --name "My App" --port 5173
> ```
>
> ```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 spa --framework vue --build-tool vite --name "My App" --port 5173
> ```
>
> > [!NOTE]
> >
> > This command will:
> >
> > 1. Check if you're authenticated (and prompt for login if needed)
> > 2. Create an Auth0 Single Page Application configured for `http://localhost:5173`
> > 3. Generate a `.env` file with `VITE_AUTH0_DOMAIN` and `VITE_AUTH0_CLIENT_ID`

> [tab] Dashboard
>
> Before you start, create a `.env` file on your project's root directory
>
> ```shellscript .env theme={null}
> VITE_AUTH0_DOMAIN=YOUR_AUTH0_APP_DOMAIN
> VITE_AUTH0_CLIENT_ID=YOUR_AUTH0_APP_CLIENT_ID
> ```
>
> 1. Head to the [Auth0 Dashboard](https://manage.auth0.com/dashboard/)
> 2. Click on **Applications** > **Applications** > **Create Application**
> 3. In the popup, enter a name for your app, select `Single Page Web Application` as the app type and click **Create**
> 4. Switch to the **Settings** tab on the Application Details page
> 5. Replace `YOUR_AUTH0_APP_DOMAIN` and `YOUR_AUTH0_APP_CLIENT_ID` on the `.env` file with the **Domain** and **Client ID** values from the dashboard
>
> Finally, on the **Settings** tab of your Application Details page, configure the following URLs:
>
> **Allowed Callback URLs:**
>
> ```
> http://localhost:5173
> ```
>
> **Allowed Logout URLs:**
>
> ```
> http://localhost:5173
> ```
>
> **Allowed Web Origins:**
>
> ```
> http://localhost:5173
> ```
>
> > [!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 Web Origins** is critical for silent authentication. Without it users will be logged out when they refresh the page or return to your app later.

> [!TIP]
>
> Verify your `.env` file exists: `cat .env` (Mac/Linux) or `type .env` (Windows)

**4. Configure the Auth0 Plugin**

```typescript src/main.ts {6,11,12,13,14,15,16,17} lines theme={null}
import './assets/main.css'

import { createApp } from 'vue'
import { createPinia } from 'pinia'
import router from './router'
import { createAuth0 } from '@auth0/auth0-vue'

import App from './App.vue'

const app = createApp(App)

app.use(createAuth0({
  domain: import.meta.env.VITE_AUTH0_DOMAIN,
  clientId: import.meta.env.VITE_AUTH0_CLIENT_ID,
  authorizationParams: {
    redirect_uri: window.location.origin
  }
}))

app.use(createPinia())
app.use(router)

app.mount('#app')
```

**5. Create Authentication Components**

Create component files

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

```powershell Windows theme={null}
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
```

And add the following code snippets

**6. Run your app**

```shellscript theme={null}
npm run dev
```

> [!NOTE]
>
> If port 5173 is in use, run: `npm run dev -- --port 5174` and update your Auth0 app's callback URLs to `http://localhost:5174`

> [!TIP]
>
> **Checkpoint**
>
> You should now have a fully functional Auth0 login page running on your [localhost](http://localhost:5173/)

***

## Advanced Usage

> [accordion] Protecting Routes with Vue Router
>
> Use Vue Router's navigation guards to protect specific routes:
>
> ```typescript src/router/index.ts theme={null}
> import { createRouter, createWebHistory } from 'vue-router'
> import { authGuard } from '@auth0/auth0-vue'
> import Home from '../views/Home.vue'
> import Profile from '../views/Profile.vue'
> import Dashboard from '../views/Dashboard.vue'
>
> const routes = [
>   {
>     path: '/',
>     name: 'Home',
>     component: Home
>   },
>   {
>     path: '/profile',
>     name: 'Profile',
>     component: Profile,
>     beforeEnter: authGuard
>   },
>   {
>     path: '/dashboard',
>     name: 'Dashboard',
>     component: Dashboard,
>     beforeEnter: authGuard
>   }
> ]
>
> const router = createRouter({
>   history: createWebHistory(),
>   routes
> })
>
> export default router
> ```

> [accordion] Calling Protected APIs
>
> Configure your Auth0 plugin to include an API audience and make authenticated requests:
>
> ```typescript src/main.ts theme={null}
> import { createAuth0 } from '@auth0/auth0-vue'
>
> const app = createApp(App)
>
> app.use(
>   createAuth0({
>     domain: import.meta.env.VITE_AUTH0_DOMAIN,
>     clientId: import.meta.env.VITE_AUTH0_CLIENT_ID,
>     authorizationParams: {
>       redirect_uri: window.location.origin,
>       audience: "YOUR_API_IDENTIFIER"
>     }
>   })
> )
> ```
>
> Then make authenticated API calls in your components:
>
> ```vue src/components/ApiCall.vue theme={null}
> <template>
>   <div>
>     <button @click="callProtectedApi" :disabled="isLoading">
>       Call Protected API
>     </button>
>     <pre v-if="apiResponse">{{ JSON.stringify(apiResponse, null, 2) }}</pre>
>   </div>
> </template>
>
> <script setup lang="ts">
> import { ref } from 'vue'
> import { useAuth0 } from '@auth0/auth0-vue'
>
> const { getAccessTokenSilently } = useAuth0()
> const apiResponse = ref(null)
> const isLoading = ref(false)
>
> const callProtectedApi = async () => {
>   try {
>     isLoading.value = true
>     const token = await getAccessTokenSilently()
>
>     const response = await fetch('/api/protected', {
>       headers: {
>         Authorization: `Bearer ${token}`
>       }
>     })
>
>     apiResponse.value = await response.json()
>   } catch (error) {
>     console.error('API call failed:', error)
>   } finally {
>     isLoading.value = false
>   }
> }
> </script>
> ```

> [accordion] Using Composables for Authentication Logic
>
> Create reusable composables for common authentication patterns:
>
> ```typescript src/composables/useAuthenticatedUser.ts theme={null}
> import { computed, ref, watchEffect } from 'vue'
> import { useAuth0 } from '@auth0/auth0-vue'
>
> export function useAuthenticatedUser() {
>   const { user, isAuthenticated, isLoading, getAccessTokenSilently } = useAuth0()
>   const accessToken = ref<string | null>(null)
>
>   watchEffect(async () => {
>     if (isAuthenticated.value && !isLoading.value) {
>       try {
>         accessToken.value = await getAccessTokenSilently()
>       } catch (error) {
>         console.error('Failed to get access token:', error)
>       }
>     }
>   })
>
>   return {
>     user: computed(() => user.value),
>     accessToken: computed(() => accessToken.value),
>     isAuthenticated: computed(() => isAuthenticated.value),
>     isLoading: computed(() => isLoading.value)
>   }
> }
> ```
>
> Usage in components:
>
> ```vue src/components/UserDashboard.vue theme={null}
> <template>
>   <div v-if="!isLoading">
>     <h1>Welcome, {{ user?.name }}</h1>
>     <p>Access Token: {{ accessToken ? 'Available' : 'Not available' }}</p>
>   </div>
>   <div v-else>Loading...</div>
> </template>
>
> <script setup lang="ts">
> import { useAuthenticatedUser } from '../composables/useAuthenticatedUser'
>
> const { user, accessToken, isLoading } = useAuthenticatedUser()
> </script>
> ```
