npx skills add https://github.com/auth0/agent-skills --skill auth0-vue使用 @auth0/auth0-vue 为 Vue.js 3 单页应用程序添加身份验证。
auth0-quickstart 技能npm install @auth0/auth0-vue
要使用 Auth0 CLI 进行自动设置,请参阅 设置指南 获取完整脚本。
手动设置:
创建 文件:
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
.envVITE_AUTH0_DOMAIN=your-tenant.auth0.com
VITE_AUTH0_CLIENT_ID=your-client-id
更新 src/main.ts:
import { createApp } from 'vue';
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.mount('#app');
创建一个登录组件:
<script setup lang="ts">
import { useAuth0 } from '@auth0/auth0-vue';
const { loginWithRedirect, logout, isAuthenticated, user, isLoading } = useAuth0();
</script>
<template>
<div>
<div v-if="isLoading">加载中...</div>
<div v-else-if="isAuthenticated">
<img :src="user?.picture" :alt="user?.name" />
<span>欢迎,{{ user?.name }}</span>
<button @click="logout({ logoutParams: { returnTo: window.location.origin }})">
登出
</button>
</div>
<button v-else @click="loginWithRedirect()">
登录
</button>
</div>
</template>
启动开发服务器并测试登录流程:
npm run dev
| 错误 | 修复方法 |
|---|---|
| 忘记在 Auth0 仪表板中添加重定向 URI | 将您的应用程序 URL(例如 http://localhost:3000、https://app.example.com)添加到 Auth0 仪表板的“允许的回调 URL”中 |
| 使用了错误的环境变量前缀 | Vite 需要 VITE_ 前缀,Vue CLI 使用 VUE_APP_ |
| 未处理加载状态 | 在渲染依赖于身份验证的 UI 之前,始终检查 isLoading |
| 将令牌存储在 localStorage 中 | 切勿手动存储令牌 - SDK 会自动处理安全存储 |
| 缺少 createAuth0 插件注册 | 必须在挂载应用之前调用 app.use(createAuth0({...})) |
| 在插件加载之前访问身份验证 | 将依赖于身份验证的代码包装在 v-if="!isLoading" 中 |
auth0-quickstart - 基础 Auth0 设置auth0-migration - 从其他身份验证提供商迁移auth0-mfa - 添加多因素身份验证核心组合式函数:
useAuth0() - 主要的身份验证组合式函数isAuthenticated - 反应式检查用户是否已登录user - 反应式用户资料信息loginWithRedirect() - 发起登录logout() - 用户登出getAccessTokenSilently() - 获取用于 API 调用的访问令牌常见用例:
每周安装量
83
仓库
GitHub 星标数
11
首次出现
2026年2月6日
安全审计
安装于
codex78
opencode78
gemini-cli77
github-copilot77
amp74
kimi-cli74
Add authentication to Vue.js 3 single-page applications using @auth0/auth0-vue.
auth0-quickstart skill firstnpm install @auth0/auth0-vue
For automated setup with Auth0 CLI , see Setup Guide for complete scripts.
For manual setup:
Create .env file:
VITE_AUTH0_DOMAIN=your-tenant.auth0.com
VITE_AUTH0_CLIENT_ID=your-client-id
Update src/main.ts:
import { createApp } from 'vue';
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.mount('#app');
Create a login component:
<script setup lang="ts">
import { useAuth0 } from '@auth0/auth0-vue';
const { loginWithRedirect, logout, isAuthenticated, user, isLoading } = useAuth0();
</script>
<template>
<div>
<div v-if="isLoading">Loading...</div>
<div v-else-if="isAuthenticated">
<img :src="user?.picture" :alt="user?.name" />
<span>Welcome, {{ user?.name }}</span>
<button @click="logout({ logoutParams: { returnTo: window.location.origin }})">
Logout
</button>
</div>
<button v-else @click="loginWithRedirect()">
Login
</button>
</div>
</template>
Start your dev server and test the login flow:
npm run dev
| Mistake | Fix |
|---|---|
| Forgot to add redirect URI in Auth0 Dashboard | Add your application URL (e.g., http://localhost:3000, https://app.example.com) to Allowed Callback URLs in Auth0 Dashboard |
| Using wrong env var prefix | Vite requires VITE_ prefix, Vue CLI uses VUE_APP_ |
| Not handling loading state | Always check isLoading before rendering auth-dependent UI |
| Storing tokens in localStorage | Never manually store tokens - SDK handles secure storage automatically |
| Missing createAuth0 plugin registration | Must call app.use(createAuth0({...})) before mounting app |
auth0-quickstart - Basic Auth0 setupauth0-migration - Migrate from another auth providerauth0-mfa - Add Multi-Factor AuthenticationCore Composables:
useAuth0() - Main authentication composableisAuthenticated - Reactive check if user is logged inuser - Reactive user profile informationloginWithRedirect() - Initiate loginlogout() - Log out usergetAccessTokenSilently() - Get access token for API callsCommon Use Cases:
Weekly Installs
83
Repository
GitHub Stars
11
First Seen
Feb 6, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykWarn
Installed on
codex78
opencode78
gemini-cli77
github-copilot77
amp74
kimi-cli74
xdrop 文件传输脚本:Bun 环境下安全上传下载工具,支持加密分享
33,800 周安装
| Accessing auth before plugin loads | Wrap auth-dependent code in v-if="!isLoading" |