npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

fastify-authjs

v1.0.9

Published

一个基于Fastify的认证插件,提供简单易用的身份验证功能

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!