nuxt-users
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseNuxt Users skill
Nuxt Users 技能
Initial setup
初始设置
-
Install the module and peer dependenciesbash
npm install nuxt-users npm install db0 better-sqlite3 bcrypt nodemailerFor MySQL or PostgreSQL, install the corresponding driver (ormysql2) instead of or in addition topgas required.better-sqlite3 -
Register the module in
nuxt.config.tstsexport default defineNuxtConfig({ modules: ['nuxt-users'] }) -
Run migrations From the project root (wherelives):
nuxt.config.tsbashnpx nuxt-users migrate -
Create at least one userbash
npx nuxt-users create-user -e admin@example.com -n "Admin User" -p password123 -r adminFlags:email,-ename,-npassword,-prole (optional).-r -
Configure permissionsts
export default defineNuxtConfig({ modules: ['nuxt-users'], nuxtUsers: { auth: { permissions: { admin: ['*'], user: ['/profile', '/api/nuxt-users/me'] } } } }) -
Use login in a page
- Use the component and handle
NUsersLoginFormby calling@successfromlogin(user).useAuthentication() - Optionally redirect after login (e.g. ).
navigateTo('/')
- Use the
-
安装模块及 peer dependenciesbash
npm install nuxt-users npm install db0 better-sqlite3 bcrypt nodemailer对于 MySQL 或 PostgreSQL,请根据需要替代或同时安装,并安装相应的驱动(better-sqlite3或mysql2)。pg -
在中注册模块
nuxt.config.tstsexport default defineNuxtConfig({ modules: ['nuxt-users'] }) -
运行数据库迁移 在项目根目录(所在位置)运行:
nuxt.config.tsbashnpx nuxt-users migrate -
至少创建一个用户bash
npx nuxt-users create-user -e admin@example.com -n "Admin User" -p password123 -r admin参数:邮箱、-e姓名、-n密码、-p角色(可选)。-r -
配置权限ts
export default defineNuxtConfig({ modules: ['nuxt-users'], nuxtUsers: { auth: { permissions: { admin: ['*'], user: ['/profile', '/api/nuxt-users/me'] } } } }) -
在页面中使用登录功能
- 使用 组件,并通过调用
NUsersLoginForm中的useAuthentication()处理login(user)。@success - 可选择在登录后进行重定向(例如 )。
navigateTo('/')
- 使用
Configuration reference (nuxt.config.ts)
配置参考(nuxt.config.ts)
All options live under in .
nuxtUsersnuxt.config.ts| Area | Key | Notes |
|---|---|---|
| Database | | |
| Database | | |
| API | | Default |
| Tables | | Custom table names |
| Mailer | | Nodemailer config for password reset emails |
| URLs | | Paths for redirects |
| Auth | | Public routes (e.g. |
| Auth | | Minutes (default 1440) |
| Auth | | Days (default 30) |
| Auth | | Role → paths (e.g. |
| Auth | | Default |
| Auth | | Google OAuth: |
| Password | | |
| Data | | |
| Locale | | Localization |
Runtime config is also supported: use for env-based or server-only settings.
runtimeConfig.nuxtUsers所有选项都位于 的 下。
nuxt.config.tsnuxtUsers| 领域 | 键 | 说明 |
|---|---|---|
| 数据库 | | |
| 数据库 | | |
| API | | 默认值为 |
| 表 | | 自定义表名 |
| 邮件服务 | | 用于密码重置邮件的 Nodemailer 配置 |
| URL | | 用于重定向的路径 |
| 身份验证 | | 公共路由(例如 |
| 身份验证 | | 分钟数(默认 1440) |
| 身份验证 | | 天数(默认 30) |
| 身份验证 | | 角色 → 路径(例如 |
| 身份验证 | | 默认值为 |
| 身份验证 | | Google OAuth: |
| 密码 | | |
| 数据 | | |
| 本地化 | | 本地化 |
也支持 runtime config:使用 进行基于环境变量或仅服务端的设置。
runtimeConfig.nuxtUsersCLI commands
CLI 命令
Run from the project root so (and optionally ) are found.
nuxt.config.ts.env-
Migrationsbash
npx nuxt-users migrate -
Create userbash
npx nuxt-users create-user -e <email> -n "<name>" -p <password> [-r <role>]Role should match akey unlessauth.permissionsisauth.allowCustomRoles(or permissions are empty).true -
Legacy/table creationbash
npx nuxt-users create-users-table npx nuxt-users create-personal-access-tokens-table npx nuxt-users create-password-reset-tokens-table npx nuxt-users create-migrations-table
Production: The CLI requires(and peers) installed where Node runs — it is not bundled insidenuxt-users. Full config needs the app root with.output/; build-only ornuxt.configdeploys fall back to--omit=devenv vars. SeeDB_*.docs/user-guide/configuration.md
请从项目根目录运行,以便找到 (以及可选的 )。
nuxt.config.ts.env-
数据库迁移bash
npx nuxt-users migrate -
创建用户bash
npx nuxt-users create-user -e <email> -n "<name>" -p <password> [-r <role>]除非为auth.allowCustomRoles(或权限配置为空),否则角色应与true的键匹配。auth.permissions -
旧版/表创建bash
npx nuxt-users create-users-table npx nuxt-users create-personal-access-tokens-table npx nuxt-users create-password-reset-tokens-table npx nuxt-users create-migrations-table
生产环境: CLI 要求在 Node 运行的位置安装(及其 peer dependencies);它不会打包在nuxt-users中。完整配置需要包含.output/的应用根目录;仅构建或使用nuxt.config的部署会回退到--omit=dev环境变量。请参阅DB_*。docs/user-guide/configuration.md
Composables (auto-imported)
Composables(自动导入)
- useAuthentication() — ,
user,isAuthenticated,login(user, rememberMe?),logout(),fetchUser(useSSR?)initializeUser() - useUsers() — Admin: ,
users,pagination,loading,error,fetchUsers(page?, limit?),updateUser,addUserremoveUser(userId) - usePublicPaths() — ,
getPublicPaths(),getAccessiblePaths(),isPublicPath(path)isAccessiblePath(path, method?) - usePasswordValidation(moduleOptions?, options?) — ,
validate(password),isValid,errors,strength,scoreclearValidation() - useNuxtUsersLocale() — ,
t(key, params?),currentLocalefallbackLocale
- useAuthentication() — 、
user、isAuthenticated、login(user, rememberMe?)、logout()、fetchUser(useSSR?)initializeUser() - useUsers() — 管理员:、
users、pagination、loading、error、fetchUsers(page?, limit?)、updateUser、addUserremoveUser(userId) - usePublicPaths() — 、
getPublicPaths()、getAccessiblePaths()、isPublicPath(path)isAccessiblePath(path, method?) - usePasswordValidation(moduleOptions?, options?) — 、
validate(password)、isValid、errors、strength、scoreclearValidation() - useNuxtUsersLocale() — 、
t(key, params?)、currentLocalefallbackLocale
Components
组件
- — Login form; use
NUsersLoginFormto call@successfromlogin(user)useAuthentication() - — Logout link/button
NUsersLogoutLink - — Display profile
NUsersProfileInfo - — Password reset form
NUsersResetPasswordForm - — List users (admin)
NUsersList - — Create/edit user form; role select from
NUsersUserFormkeys; setauth.permissionsfor a custom free-text role optionauth.allowCustomRoles: true
- — 登录表单;使用
NUsersLoginForm调用@success中的useAuthentication()login(user) - — 退出登录链接/按钮
NUsersLogoutLink - — 显示个人资料
NUsersProfileInfo - — 密码重置表单
NUsersResetPasswordForm - — 用户列表(管理员)
NUsersList - — 创建/编辑用户表单;角色选项来自
NUsersUserForm的键;设置auth.permissions可使用自定义自由文本角色选项auth.allowCustomRoles: true
Troubleshooting
故障排除
| Symptom | Fix |
|---|---|
| Redirected to login on protected routes | Set |
| CLI config not found / wrong tables | Run CLI from the directory containing |
| Migrations table missing | Run |
| Database driver errors | Install correct peer: SQLite → |
| 症状 | 修复方法 |
|---|---|
| 在受保护的路由上被重定向到登录页 | 设置 |
| 找不到 CLI 配置或使用了错误的表 | 在包含 |
| 缺少迁移表 | 在项目根目录运行 |
| 数据库驱动错误 | 安装正确的 peer dependency:SQLite → |
File references
文件参考
- Project LLM context and config types: llms.txt in the repo root
- Full docs: https://nuxt-users.webmania.cc/
- Getting started and examples: ,
../../docs/user-guide/getting-started.md../../docs/examples/basic-setup.md - Authorization:
../../docs/user-guide/authorization.md - Configuration details:
../../docs/user-guide/configuration.md
Keep config and permissions in sync with the app’s roles and routes; use guard clauses and early returns when implementing custom auth logic.
nuxtUsers- 项目 LLM 上下文和配置类型:仓库根目录中的 llms.txt
- 完整文档:https://nuxt-users.webmania.cc/
- 入门和示例:、
../../docs/user-guide/getting-started.md../../docs/examples/basic-setup.md - 授权:
../../docs/user-guide/authorization.md - 配置详情:
../../docs/user-guide/configuration.md
确保 配置和权限与应用的角色及路由保持同步;实现自定义身份验证逻辑时,使用 guard clauses 和 early return。
nuxtUsers