重要前提
安装AI Skills的关键前提是:必须科学上网,且开启TUN模式,这一点至关重要,直接决定安装能否顺利完成,在此郑重提醒三遍:科学上网,科学上网,科学上网。查看完整安装教程 →
auth0-fastify by auth0/agent-skills
npx skills add https://github.com/auth0/agent-skills --skill auth0-fastify使用 @auth0/auth0-fastify 为 Fastify Web 应用程序添加身份验证。
auth0-quickstart 技能auth0-react、auth0-vue 或 auth0-angularauth0-nextjs 技能,它同时处理客户端和服务器端auth0-react-native广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
@auth0/auth0-fastify-apinpm install @auth0/auth0-fastify fastify @fastify/view ejs dotenv
创建 .env 文件:
AUTH0_DOMAIN=your-tenant.auth0.com
AUTH0_CLIENT_ID=your-client-id
AUTH0_CLIENT_SECRET=your-client-secret
SESSION_SECRET=<openssl-rand-hex-64>
APP_BASE_URL=http://localhost:3000
生成密钥:openssl rand -hex 64
创建你的 Fastify 服务器 (server.js):
import 'dotenv/config';
import Fastify from 'fastify';
import fastifyAuth0 from '@auth0/auth0-fastify';
import fastifyView from '@fastify/view';
import ejs from 'ejs';
const fastify = Fastify({ logger: true });
// 注册视图引擎
await fastify.register(fastifyView, {
engine: { ejs },
root: './views',
});
// 配置 Auth0 插件
await fastify.register(fastifyAuth0, {
domain: process.env.AUTH0_DOMAIN,
clientId: process.env.AUTH0_CLIENT_ID,
clientSecret: process.env.AUTH0_CLIENT_SECRET,
appBaseUrl: process.env.APP_BASE_URL,
sessionSecret: process.env.SESSION_SECRET,
});
fastify.listen({ port: 3000 });
这将自动创建:
/auth/login - 登录端点/auth/logout - 登出端点/auth/callback - OAuth 回调端点// 公开路由
fastify.get('/', async (request, reply) => {
const session = await fastify.auth0Client.getSession({ request, reply });
return reply.view('views/home.ejs', {
isAuthenticated: !!session,
});
});
// 受保护的路由
fastify.get('/profile', {
preHandler: async (request, reply) => {
const session = await fastify.auth0Client.getSession({ request, reply });
if (!session) {
return reply.redirect('/auth/login');
}
}
}, async (request, reply) => {
const user = await fastify.auth0Client.getUser({ request, reply });
return reply.view('views/profile.ejs', { user });
});
启动你的服务器:
node server.js
访问 http://localhost:3000 并测试登录流程。
| 错误 | 修复方法 |
|---|---|
| 忘记在 Auth0 仪表板中添加回调 URL | 将 /auth/callback 路径添加到允许的回调 URL 中(例如,http://localhost:3000/auth/callback) |
| 缺少或强度不足的 SESSION_SECRET | 使用 openssl rand -hex 64 生成安全的 64 字符密钥并存储在 .env 文件中 |
| 在 Auth0 中创建为 SPA 类型应用程序 | 对于服务器端身份验证,必须是常规 Web 应用程序类型 |
| 会话密钥在代码中暴露 | 始终使用环境变量,切勿硬编码密钥 |
| 生产环境使用了错误的 appBaseUrl | 将 APP_BASE_URL 更新为匹配你的生产域名 |
| 没有 await fastify.register | Fastify v4+ 要求 await 插件注册 |
auth0-quickstart - 基础 Auth0 设置auth0-migration - 从其他身份验证提供商迁移auth0-mfa - 添加多因素身份验证插件选项:
domain - Auth0 租户域名(必需)clientId - Auth0 客户端 ID(必需)clientSecret - Auth0 客户端密钥(必需)appBaseUrl - 应用程序 URL(必需)sessionSecret - 会话加密密钥(必需,最少 64 字符)audience - API 受众(可选,用于调用 API)客户端方法:
fastify.auth0Client.getSession({ request, reply }) - 获取用户会话fastify.auth0Client.getUser({ request, reply }) - 获取用户资料fastify.auth0Client.getAccessToken({ request, reply }) - 获取访问令牌fastify.auth0Client.logout(options, { request, reply }) - 登出用户常见用例:
preHandler 检查会话(参见步骤 4)!!sessiongetUser({ request, reply })getAccessToken({ request, reply })每周安装数
67
仓库
GitHub 星标数
13
首次出现
2026年2月27日
安全审计
安装于
cursor66
gemini-cli66
github-copilot66
amp66
codex66
kimi-cli66
Add authentication to Fastify web applications using @auth0/auth0-fastify.
auth0-quickstart skill firstauth0-react, auth0-vue, or auth0-angular for client-side authauth0-nextjs skill which handles both client and serverauth0-react-native for React Native/Expo@auth0/auth0-fastify-api instead for JWT validation without sessionsnpm install @auth0/auth0-fastify fastify @fastify/view ejs dotenv
Create .env:
AUTH0_DOMAIN=your-tenant.auth0.com
AUTH0_CLIENT_ID=your-client-id
AUTH0_CLIENT_SECRET=your-client-secret
SESSION_SECRET=<openssl-rand-hex-64>
APP_BASE_URL=http://localhost:3000
Generate secret: openssl rand -hex 64
Create your Fastify server (server.js):
import 'dotenv/config';
import Fastify from 'fastify';
import fastifyAuth0 from '@auth0/auth0-fastify';
import fastifyView from '@fastify/view';
import ejs from 'ejs';
const fastify = Fastify({ logger: true });
// Register view engine
await fastify.register(fastifyView, {
engine: { ejs },
root: './views',
});
// Configure Auth0 plugin
await fastify.register(fastifyAuth0, {
domain: process.env.AUTH0_DOMAIN,
clientId: process.env.AUTH0_CLIENT_ID,
clientSecret: process.env.AUTH0_CLIENT_SECRET,
appBaseUrl: process.env.APP_BASE_URL,
sessionSecret: process.env.SESSION_SECRET,
});
fastify.listen({ port: 3000 });
This automatically creates:
/auth/login - Login endpoint/auth/logout - Logout endpoint/auth/callback - OAuth callback// Public route
fastify.get('/', async (request, reply) => {
const session = await fastify.auth0Client.getSession({ request, reply });
return reply.view('views/home.ejs', {
isAuthenticated: !!session,
});
});
// Protected route
fastify.get('/profile', {
preHandler: async (request, reply) => {
const session = await fastify.auth0Client.getSession({ request, reply });
if (!session) {
return reply.redirect('/auth/login');
}
}
}, async (request, reply) => {
const user = await fastify.auth0Client.getUser({ request, reply });
return reply.view('views/profile.ejs', { user });
});
Start your server:
node server.js
Visit http://localhost:3000 and test the login flow.
| Mistake | Fix |
|---|---|
| Forgot to add callback URL in Auth0 Dashboard | Add /auth/callback path to Allowed Callback URLs (e.g., http://localhost:3000/auth/callback) |
| Missing or weak SESSION_SECRET | Generate secure 64-char secret with openssl rand -hex 64 and store in .env |
| App created as SPA type in Auth0 | Must be Regular Web Application type for server-side auth |
| Session secret exposed in code | Always use environment variables, never hardcode secrets |
| Wrong appBaseUrl for production | Update APP_BASE_URL to match your production domain |
| Not awaiting fastify.register | Fastify v4+ requires awaiting plugin registration |
auth0-quickstart - Basic Auth0 setupauth0-migration - Migrate from another auth providerauth0-mfa - Add Multi-Factor AuthenticationPlugin Options:
domain - Auth0 tenant domain (required)clientId - Auth0 client ID (required)clientSecret - Auth0 client secret (required)appBaseUrl - Application URL (required)sessionSecret - Session encryption secret (required, min 64 chars)audience - API audience (optional, for calling APIs)Client Methods:
fastify.auth0Client.getSession({ request, reply }) - Get user sessionfastify.auth0Client.getUser({ request, reply }) - Get user profilefastify.auth0Client.getAccessToken({ request, reply }) - Get access tokenfastify.auth0Client.logout(options, { request, reply }) - Logout userCommon Use Cases:
preHandler to check session (see Step 4)!!sessiongetUser({ request, reply })getAccessToken({ request, reply })Weekly Installs
67
Repository
GitHub Stars
13
First Seen
Feb 27, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
cursor66
gemini-cli66
github-copilot66
amp66
codex66
kimi-cli66
浏览器自动化策略指南:何时及如何使用实时浏览器会话进行网页调试与研究
45,600 周安装
智能体性能优化指南:通过数据驱动和提示工程提升AI智能体可靠性与效率
288 周安装
AgentOps技能转换器 - 一键将技能转换为Codex、Cursor等AI平台格式
288 周安装
goals by boshu2/agentops:自动化健身目标维护与测量CLI工具
289 周安装
opencode-mirror 镜像工具:快速配置与安全使用指南 | Git 镜像管理
296 周安装
heal-skill:自动化技能维护工具,一键检测修复技能规范问题
293 周安装
LobeChat i18n 国际化指南:使用 react-i18next 实现多语言支持与最佳实践
295 周安装