跳到正文
Skillpedia
安全25k · #10089

nocobase-app-plugin-authentication

它能做什么

这项技能指导 NocoBase 3 应用中的身份验证工作,使用 Better Auth。适合需要保护页面、自定义登录或添加登录方式的开发者。

适用场景

  • ✓当你需要保护 API 路由或页面,使其只有登录用户才能使用时。
  • ✓当你想为应用添加社交登录或 OIDC 登录时。
  • ✓当你想自定义登录、注册或密码页面时。

技能链接

https://github.com/nocobase/nocobase/blob/v3-develop/packages/plugins/app-plugin-authentication/skills/nocobase-app-plugin-authentication/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/nocobase/nocobase/blob/v3-develop/packages/plugins/app-plugin-authentication/skills/nocobase-app-plugin-authentication/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“请帮我为 NocoBase 应用添加 Google 登录,并让管理员页面要求用户先登录。”

每周 5 个新技能,1 分钟读完。

工作原理

AI 会通过 Better Auth 配置添加登录方式,并利用会话保护路由。然后根据需要自定义登录页面。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Authentication development

Use this Skill when an application built on @nocobase/app-plugin-authentication needs anything about who the caller is: protecting a route or page, reading the current user, changing the sign-in pages, adding a sign-in method, disabling accounts, or preparing authentication for deployment.

Do not use it for what a signed-in user may do — that is nocobase-app-plugin-authorization — or for the user administration page, which is nocobase-app-plugin-users. Do not modify the plugin's own source, migrations, or dist/; everything below is done in the application.

The plugin wraps Better Auth. Application configuration is Better Auth configuration, and every Better Auth plugin, social provider, and hook is available through it. Read the installed Better Auth version's documentation for option details; this Skill covers where those options go in a NocoBase application and what the plugin adds on top.

Enable an official Better Auth plugin before writing anything of your own: device login for a CLI (deviceAuthorization() with bearer()), organizations, two-factor and passkeys all exist upstream, and each needs only configuration, an application migration for its tables, and its UI. Enabling an official Better Auth plugin covers the rule, the schema check and the migration.

Public surfaces

Server, from @nocobase/app-plugin-authentication (the root entry is the server entry) or @nocobase/app-plugin-authentication/server:

  • authenticationToken resolves the Auth instance: required(), optional(), getSession(headers), handler(request), signInUser(userId, request).
  • AuthEnv types a Hono router whose routes read context.get('auth').
  • userAdministrationServiceToken resolves UserAdministrationService: list, get, create, update, disable, enable, resetPassword, revokeSessions, withConnection; errors are UserAdministrationError.
  • AuthConfig is Better Auth's BetterAuthOptions, the type of the application's server/config/auth.ts. User initialization configuration lives separately under users.initialAdmin.
  • createAuthentication, databaseAdapter, createAuthStorage build an instance outside the application runtime, mainly in tests.

Client, from @nocobase/app-plugin-authentication/client:

  • authentication() is the registration factory. It takes no options, contributes no routes, and mounts one React provider.
  • useAuthentication() returns { client, session, isPending, refresh }; useAuthenticationClient() returns the Better Auth client alone.
  • RequiredAuthentication, GuestAuthentication, and AuthenticationGuard({ mode }) gate a route subtree on the session.
  • AuthConfig is Better Auth's client options, the type of the application's client/config/auth.ts.
  • authenticationClientToken is the client's service token, used to stub the client in component tests.

Headless actions, from @nocobase/app-plugin-authentication/client/actions: usePasswordLogin, usePasswordRegistration, usePasswordResetRequest, usePasswordReset, each returning { submit, isPending, error }.

HTTP: every Better Auth endpoint is served under /api/auth/* by the plugin's own route. A Better Auth plugin's endpoints appear there automatically. There is no other authentication REST surface.

API document: the plugin lets a signed-in session read the App's OpenAPI document at GET /api/swagger and its Swagger UI at GET /api/swagger/docs; an anonymous request gets 401 with error.reason API_DOCS_UNAUTHENTICATED, and @nocobase/app-plugin-api-keys adds API keys as a second way in. The plugin also merges Better Auth's endpoints into that document from Better Auth's own OpenAPI generator, at their full /api/auth/... paths and tagged Authentication, so a Better Auth plugin added to auth.plugins is documented without further work. Browser-only steps are left out: /sign-in/social, /link-social, /callback/{id}, /verify-email, /reset-password/{token}, /delete-user/callback and /error. Do not add Better Auth's openAPI() plugin: it would publish its own unauthenticated /api/auth/reference page and schema route.

Choose the task path

| The task is | Read | | ---------------------------------------------------

常见问题

这项技能决定用户能做什么吗?

不决定。登录用户可以做什么,由授权技能负责。

我能添加双重验证或通行密钥(passkeys)吗?

可以。技能涵盖启用 Better Auth 官方插件,例如双重验证和通行密钥。

它会修改插件本身的代码吗?

不会。所有更改都放在你的应用中。

详情

作者的原始描述

Build authentication requirements in a NocoBase 3 application: protect API routes and pages with the session, read and refresh the session in the browser, customize the login, registration and password pages, add sign-in methods such as social login, OIDC or a custom Better Auth plugin, enable official Better Auth plugins (device login for a CLI, bearer tokens, organizations, two-factor, passkeys) with the migration their tables need, manage account lifecycle from server code, and configure secrets, cookies and shared storage for deployment.

repo: nocobase/nocobase

热度

GitHub 星标
25k
排名
#10089
许可证
—
更新时间
2026/11/2

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能