重要前提
安装AI Skills的关键前提是:必须科学上网,且开启TUN模式,这一点至关重要,直接决定安装能否顺利完成,在此郑重提醒三遍:科学上网,科学上网,科学上网。查看完整安装教程 →
auth0-fastify-api by auth0/agent-skills
npx skills add https://github.com/auth0/agent-skills --skill auth0-fastify-api使用 @auth0/auth0-fastify-api 通过 JWT 访问令牌验证来保护 Fastify API 端点。
auth0-quickstart 技能@auth0/auth0-fastifyauth0-react、auth0-vue 或 auth0-angularauth0-nextjs 技能广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
auth0-react-nativenpm install @auth0/auth0-fastify-api fastify dotenv
您需要在 Auth0 中创建一个 API (而不是 Application):
# 使用 Auth0 CLI
auth0 apis create \
--name "My Fastify API" \
--identifier https://my-api.example.com
或者在 Auth0 仪表板中手动创建:Applications → APIs
创建 .env 文件:
AUTH0_DOMAIN=your-tenant.auth0.com
AUTH0_AUDIENCE=https://my-api.example.com
创建您的 Fastify 服务器 (server.js):
import 'dotenv/config';
import Fastify from 'fastify';
import fastifyAuth0Api from '@auth0/auth0-fastify-api';
const fastify = Fastify({ logger: true });
// 注册 Auth0 API 插件
await fastify.register(fastifyAuth0Api, {
domain: process.env.AUTH0_DOMAIN,
audience: process.env.AUTH0_AUDIENCE,
});
fastify.listen({ port: 3001 });
// 公共路由 - 无需身份验证
fastify.get('/api/public', async (request, reply) => {
return {
message: 'Hello from a public endpoint!',
timestamp: new Date().toISOString(),
};
});
// 受保护的路由 - 需要有效的 JWT
fastify.get('/api/private', {
preHandler: fastify.requireAuth()
}, async (request, reply) => {
return {
message: 'Hello from a protected endpoint!',
user: request.user.sub,
timestamp: new Date().toISOString(),
};
});
// 包含用户信息的受保护路由
fastify.get('/api/profile', {
preHandler: fastify.requireAuth()
}, async (request, reply) => {
return {
profile: request.user, // JWT 声明
};
});
测试公共端点:
curl http://localhost:3001/api/public
测试受保护的端点 (需要访问令牌):
curl http://localhost:3001/api/private \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN"
| 错误 | 修复方法 |
|---|---|
| 在 Auth0 中创建了 Application 而不是 API | 必须在 Auth0 仪表板 → Applications → APIs 中创建 API 资源 |
| 缺少 Authorization 请求头 | 在所有受保护端点的请求中包含 Authorization: Bearer <token> |
| 令牌中的 audience 错误 | 客户端请求令牌时必须使用匹配的 audience 参数 |
| 使用 ID 令牌而不是访问令牌 | API 身份验证必须使用访问令牌,而不是 ID 令牌 |
| 未处理 401/403 错误 | 为未授权/禁止的响应实现适当的错误处理 |
auth0-quickstart - 基础 Auth0 设置auth0-fastify - 用于具有会话功能的服务器端渲染 Fastify Web 应用程序auth0-mfa - 添加多因素身份验证插件选项:
domain - Auth0 租户域名 (必需)audience - 来自 Auth0 API 设置的 API 标识符 (必需)请求属性:
request.user - 解码后的 JWT 声明对象request.user.sub - 用户 ID (主题)中间件:
fastify.requireAuth() - 使用 JWT 验证保护路由fastify.requireAuth({ scopes: 'read:data' }) - 要求特定权限范围fastify.requireAuth({ scopes: ['read:data', 'write:data'] }) - 要求特定权限范围常见用例:
preHandler: fastify.requireAuth() (参见步骤 5)request.user.subrequest.user['namespace/claim'] 访问每周安装次数
19
仓库
GitHub 星标数
10
首次出现
11 天前
安全审计
已安装于
opencode19
gemini-cli19
github-copilot19
amp19
cline19
codex19
Protect Fastify API endpoints with JWT access token validation using @auth0/auth0-fastify-api.
auth0-quickstart skill first@auth0/auth0-fastify for session-based authauth0-react, auth0-vue, or auth0-angular for client-side authauth0-nextjs skillauth0-react-native for React Native/Exponpm install @auth0/auth0-fastify-api fastify dotenv
You need an API (not Application) in Auth0:
# Using Auth0 CLI
auth0 apis create \
--name "My Fastify API" \
--identifier https://my-api.example.com
Or create manually in Auth0 Dashboard → Applications → APIs
Create .env:
AUTH0_DOMAIN=your-tenant.auth0.com
AUTH0_AUDIENCE=https://my-api.example.com
Create your Fastify server (server.js):
import 'dotenv/config';
import Fastify from 'fastify';
import fastifyAuth0Api from '@auth0/auth0-fastify-api';
const fastify = Fastify({ logger: true });
// Register Auth0 API plugin
await fastify.register(fastifyAuth0Api, {
domain: process.env.AUTH0_DOMAIN,
audience: process.env.AUTH0_AUDIENCE,
});
fastify.listen({ port: 3001 });
// Public route - no authentication
fastify.get('/api/public', async (request, reply) => {
return {
message: 'Hello from a public endpoint!',
timestamp: new Date().toISOString(),
};
});
// Protected route - requires valid JWT
fastify.get('/api/private', {
preHandler: fastify.requireAuth()
}, async (request, reply) => {
return {
message: 'Hello from a protected endpoint!',
user: request.user.sub,
timestamp: new Date().toISOString(),
};
});
// Protected route with user info
fastify.get('/api/profile', {
preHandler: fastify.requireAuth()
}, async (request, reply) => {
return {
profile: request.user, // JWT claims
};
});
Test public endpoint:
curl http://localhost:3001/api/public
Test protected endpoint (requires access token):
curl http://localhost:3001/api/private \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN"
| Mistake | Fix |
|---|---|
| Created Application instead of API in Auth0 | Must create API resource in Auth0 Dashboard → Applications → APIs |
| Missing Authorization header | Include Authorization: Bearer <token> in all protected endpoint requests |
| Wrong audience in token | Client must request token with matching audience parameter |
| Using ID token instead of access token | Must use access token for API auth, not ID token |
| Not handling 401/403 errors | Implement proper error handling for unauthorized/forbidden responses |
auth0-quickstart - Basic Auth0 setupauth0-fastify - For server-rendered Fastify web apps with sessionsauth0-mfa - Add Multi-Factor AuthenticationPlugin Options:
domain - Auth0 tenant domain (required)audience - API identifier from Auth0 API settings (required)Request Properties:
request.user - Decoded JWT claims objectrequest.user.sub - User ID (subject)Middleware:
fastify.requireAuth() - Protect route with JWT validationfastify.requireAuth({ scopes: 'read:data' }) - Require specific scopefastify.requireAuth({ scopes: ['read:data', 'write:data'] }) - Require specific scopesCommon Use Cases:
preHandler: fastify.requireAuth() (see Step 5)request.user.subrequest.user['namespace/claim']Weekly Installs
19
Repository
GitHub Stars
10
First Seen
11 days ago
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode19
gemini-cli19
github-copilot19
amp19
cline19
codex19
Linux云主机安全托管指南:从SSH加固到HTTPS部署
50,200 周安装
开发者成长分析工具 - 基于AI的编码模式分析与个性化学习建议
103 周安装
Gemini API 音频视频转录工具 - 支持 YouTube URL 和本地文件,输出结构化 Markdown 字幕
103 周安装
Bun 专家指南:运行时、包管理、测试与打包工具全解析 | 高性能 JavaScript/TypeScript 开发
103 周安装
QR/条形码读取器 - 支持多格式批量扫描的Python库,自动图像预处理与数据导出
103 周安装
前端设计技能:创建独特、生产级UI界面,告别AI生成美学,实现创意前端开发
103 周安装
A股量化分析必备:AkShare股票数据接口使用指南,实时行情、历史K线、财务数据、资金流向全解析
105 周安装