fastify-authjs
v1.0.9
Published
一个基于Fastify的认证插件,提供简单易用的身份验证功能
Maintainers
Readme
Fastify AuthJS
一个基于Fastify的认证插件,提供简单易用的身份验证功能。
特性
- 🔐 简单易用的身份验证
- 🔧 可配置的认证选项
- 🛡️ 支持路由级别的认证控制
- 📦 支持ESM和CommonJS两种用法
- 📝 完整的TypeScript类型支持
- 🧪 完整的测试覆盖
安装
npm install fastify-authjs使用
基本用法
// CommonJS
const fastify = require('fastify')
const fastifyAuthJs = require('fastify-authjs')
const app = fastify()
// 注册插件
app.register(fastifyAuthJs, {
secret: 'my-secret-key',
enabled: true
})
// 添加一个需要认证的路由
app.get('/protected', {
config: {
needsAuth: true
}
}, async (request, reply) => {
return {
message: 'This is a protected route',
user: request.user
}
})
// 添加一个公开路由
app.get('/public', {
config: {
needsAuth: false
}
}, async (request, reply) => {
return {
message: 'This is a public route, no authentication needed'
}
})
// 启动服务器
app.listen({ port: 3000 }, (err) => {
if (err) {
console.error(err)
process.exit(1)
}
console.log('Server listening on port 3000')
})ESM用法
// ESM
import fastify from 'fastify'
import fastifyAuthJs from 'fastify-authjs'
const app = fastify()
// 注册插件
await app.register(fastifyAuthJs, {
secret: 'my-secret-key',
enabled: true
})
// 添加路由...TypeScript用法
import fastify from 'fastify'
import fastifyAuthJs, { FastifyAuthJsOptions } from 'fastify-authjs'
const app = fastify()
// 注册插件
const options: FastifyAuthJsOptions = {
secret: 'my-secret-key',
enabled: true
}
await app.register(fastifyAuthJs, options)
// 添加路由...API
插件选项
| 选项 | 类型 | 默认值 | 描述 |
| --- | --- | --- | --- |
| secret | string | 'default-secret-key' | 认证密钥 |
| enabled | boolean | true | 是否启用认证 |
| onAuthFailed | function | undefined | 认证失败时的自定义处理函数 |
路由配置
| 配置 | 类型 | 默认值 | 描述 |
| --- | --- | --- | --- |
| config.needsAuth | boolean | true | 路由是否需要认证 |
认证方式
插件使用Bearer Token认证方式。在请求头中添加Authorization字段,值为Bearer <token>。
curl -X GET http://localhost:3000/protected \
-H "Authorization: Bearer valid-token"自定义认证失败处理
app.register(fastifyAuthJs, {
secret: 'my-secret-key',
enabled: true,
onAuthFailed: (request, reply) => {
reply.code(401).send({
error: 'Custom Unauthorized',
message: 'You need to be authenticated to access this resource'
})
}
})禁用认证
app.register(fastifyAuthJs, {
secret: 'my-secret-key',
enabled: false
})示例
完整示例
const fastify = require('fastify')
const fastifyAuthJs = require('fastify-authjs')
const app = fastify({
logger: true
})
// 注册认证插件
app.register(fastifyAuthJs, {
secret: 'my-secret-key',
enabled: true,
onAuthFailed: (request, reply) => {
reply.code(401).send({
error: 'Custom Unauthorized',
message: 'You need to be authenticated to access this resource'
})
}
})
// 添加一个需要认证的路由
app.get('/protected', {
config: {
needsAuth: true
}
}, async (request, reply) => {
return {
message: 'This is a protected route',
user: request.user
}
})
// 添加一个公开路由
app.get('/public', {
config: {
needsAuth: false
}
}, async (request, reply) => {
return {
message: 'This is a public route, no authentication needed'
}
})
// 启动服务器
const start = async () => {
try {
await app.listen({ port: 3000 })
console.log('Server listening on port 3000')
} catch (err) {
console.error(err)
process.exit(1)
}
}
start()开发
安装依赖
npm install构建项目
npm run build运行测试
npm test运行示例
npm run dev代码检查
npm run lint修复代码问题
npm run lint:fix许可证
MIT
贡献
欢迎提交Issue和Pull Request!
