@freebuff/auth

中文文档
0.1.0发布于 2026/10/07Apache-2.0TypeScriptES Module近 30 天下载 2.3万

与 Freebuff 签约 : 在任何没有秘密的网站中加入 Freebuff 账户 。 OIDC + Cloudflare工人的PKCE, Bun, Node 和 Next.js.

中文使用文档
文档目录9 节
  1. 1. 获得客户身份
  2. 2. 上马
  3. 云旗工人、邦克、德诺、霍诺
  4. Next.js (App路由器) (英语)
  5. 按钮和客户端状态( 反应)
  6. 用户
  7. 会议和安全
  8. 地方发展
  9. 选项

与 Freebuff 签约

@freebuff/auth 让有Freebuff帐号的人进入您的网站: Google、 GitHub 或 Apple 通过 Freebuff , 没有配置, 没有 秘密要保密 秘密要保密 您的服务器了解访问者是谁( Q)sub,并与其 同意他们的电子邮件、姓名和图片)。 它从来没有看到他们的密码, 提供商证书或Freebuff账户代号.

这是标准的 OpenID 连接(授权代码 + PKCE),带有 服务器侧会话,并随处运行 fetch 和网络加密 : Cloudflare Workers, Bun, Node 20+, Deno and Next.js. 互联网档案馆的存檔,存档日期2013-12-22.

1. 获得客户身份

每个项目环境都有自己的公共客户身份证,由 project的创建者针对精确的回调 URL :

# Production: the site's real origin
npx @freebuff/auth register --project notes --name "Notes" \
  --environment production --redirect-uri https://notes.example/auth/freebuff/callback

# Local development: any port on localhost
npx @freebuff/auth register --project notes --name "Notes" \
  --environment development --redirect-uri http://localhost:8787/auth/freebuff/callback

它以您的 Freebuff CLI 登录方式( 或 FREEBUFF_TOKEN和 指纹 FREEBUFF_CLIENT_ID以及你网站的出处 这里有配置 也不是一个秘密。 重新运行是安全的; 添加一个 新设 --redirect-uri 当网站获得新域名时.

2. 上马

云旗工人、邦克、德诺、霍诺

包好你的接线员 它服务于四个认证路线,维持会话 新鲜的,和制作 getUser 可在内部任何地方使用:

import { createFreebuffAuth } from '@freebuff/auth'

const auth = createFreebuffAuth({
  clientId: env.FREEBUFF_CLIENT_ID,
  baseUrl: 'https://notes.example', // your canonical origin
})

export default {
  fetch: auth.wrap(async (request) => {
    const user = await auth.getUser(request)
    if (new URL(request.url).pathname.startsWith('/api/') && !user) {
      return Response.json({ error: 'Sign in required' }, { status: 401 })
    }
    return app.fetch(request)
  }),
}

见 examples/cloudflare-worker.ts (静态) 资产+API)和 examples/hono.ts。 。 。 .

Next.js (App路由器) (英语)

// lib/freebuff.ts
import { createFreebuffNext } from '@freebuff/auth/next'
export const freebuff = createFreebuffNext({
  clientId: process.env.FREEBUFF_CLIENT_ID!,
  baseUrl: process.env.APP_ORIGIN!,
})

// app/auth/freebuff/[...freebuff]/route.ts
export const { GET, POST } = freebuff.handlers

// proxy.ts (middleware.ts before Next 16): refreshes sessions before render
export const proxy = freebuff.proxy
export const config = { matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'] }

// Any Server Component, Route Handler or Server Action
const user = await freebuff.getUser()

见 examples/nextjs。 。 。 .

按钮和客户端状态( 反应)

import { SignInWithFreebuff, SignOutButton, useFreebuffUser } from '@freebuff/auth/react'

<SignInWithFreebuff />                 // link to /auth/freebuff/signin, returns here
<SignOutButton returnTo="/" />         // a POST form; sign-out is never a link
const { user, isLoading } = useFreebuffUser() // reads /auth/freebuff/session

无反应: 链接到 /auth/freebuff/signin?returnTo=/somewhere签名 与 <form method="post" action="/auth/freebuff/signout">,并读 GET /auth/freebuff/session → { "user": { ... } | null }。 。 。 .

用户

interface FreebuffUser {
  sub: string            // always; stable for this visitor in this project
  email?: string         // `email` scope, when present
  email_verified?: boolean
  name?: string          // `profile` scope, when set
  picture?: string       // https URL
}
  • 键入您的数据 sub。 。 。 . 你的项目也一样 开发和生产客户,以及所有其他项目的不同, 网站无法与访客联系 它永远不会改变.
  • 电子邮件是配置数据,不是身份. 它可以改变,可能缺席,以及 可能无法核实。 从不通过电子邮件合并或查询账户 .
  • 请求少用 scopes: ['openid'] (刚刚) sub)或(或) ['openid', 'email']。 。 。 .

会议和安全

  • 你的项目没有秘密. Freebuff 签名符; 您 服务器用 Freebuff 的公钥验证它们( 已选中; 无请求) 网络呼叫.
  • 这届会议是一场 __Host-freebuff_session 饼干: HttpOnly, (中文(简体) ), Secure, (中文(简体) ), SameSite=Lax仅限主机。 它有一个一小时的进入符和一个刷新 符号 。 托肯斯从未达到页面JavaScript.
  • 刷新符号在每次使用时旋转 。 一个被窃取的、已经使用的刷新符号 结束整个会话。 平行请求是安全的:收到相同的请求 继任者.
  • 会期最长可达30天无活动,最多可达90天,然后 访客再次签名(通常是一击:Freebuff记得同意).
  • 签出 POST,只有同源;它撤销了Freebuff的会话.
  • 如果禁用 Freebuff 账户, 或项目禁用, 则其会话 在一个小时之内(进入-连接寿命)停止,不能更新.
  • 登录使用 PKCE, state 财务报告和审定财务报表 nonce固定的发行者 准确的回调URL, 和RFC 9207 发行商检查。 浏览器拒绝托肯请求 .

getUser 只在内部刷新 wrap,下一个.js proxy,或 /auth/freebuff/session 路由,因为更新的会话必须到达 浏览器或它的下个刷新 看起来像偷窃。 在其他地方,它验证 当前访问符和返回 null 一旦它过期.

地方发展

使用您的项目 发展 客户身份和 allowInsecureLocalhost: true 带有 http://localhost:<port> 或者说 http://127.0.0.1:<port> baseUrl. 开发客户接受任何港口 在已注册回回路主机上。 永远不要使用开发客户端或 allowInsecureLocalhost 在生产中.

选项

选项默认
clientId所需经费公众,从提供
baseUrl所需经费名称来源;从未根据请求获得
issuerhttps://freebuff.com/oidc仅供非生产的 Freebuff
scopes['openid', 'email', 'profile']openid 总是包含
allowInsecureLocalhostfalse只发展
fetch全球 fetch服务器端呼叫 Freebuff

将整合此软件包的 AI 编码代理器改为: AGENTS.md。 。 。 .

版本与发布记录

@freebuff/auth 共发布 1 个版本,首次发布于 2026/10/07;过去 12 个月发布了 1 个版本。

版本发布日期距今
0.1.0latest2026/10/071 个月前

关于 @freebuff/auth 的常见问题

@freebuff/auth 是什么?

与 Freebuff 签约 : 在任何没有秘密的网站中加入 Freebuff 账户 。 OIDC + Cloudflare工人的PKCE, Bun, Node 和 Next.js.

如何安装 @freebuff/auth?

在项目目录运行 npm install @freebuff/auth,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 freebuff-auth,可以用 npx freebuff-auth 直接运行。

@freebuff/auth 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@freebuff/auth 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@freebuff/auth 可以商用吗?

@freebuff/auth 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@freebuff/auth 的下载量有多少?

根据 npm 官方下载统计,@freebuff/auth 在 2026/10/16 – 2026/11/14 这 30 天内被下载 22,946 次,日均约 765 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@freebuff/auth 还在维护吗?

最新版本 0.1.0 发布于 2026/10/07(1 个月前);至今共发布 1 个版本,其中过去 12 个月发布了 1 个;首次发布于 2026/10/07。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。