clerk-nextjs-patterns
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseNext.js Patterns
Next.js 模式
For basic setup, see .
setup/基础设置请查看目录。
setup/Impact Levels
影响等级
- CRITICAL - Breaking bugs, security holes
- HIGH - Common mistakes
- MEDIUM - Optimization
- CRITICAL(严重) - 破坏性bug、安全漏洞
- HIGH(高) - 常见错误
- MEDIUM(中等) - 优化项
References
参考资料
| Reference | Impact |
|---|---|
| CRITICAL - |
| HIGH - Public-first vs protected-first |
| HIGH - Protect mutations |
| HIGH - 401 vs 403 |
| MEDIUM - User-scoped caching |
| 参考文档 | 影响等级 |
|---|---|
| CRITICAL - |
| HIGH - 优先公开 vs 优先保护 |
| HIGH - 保护突变操作 |
| HIGH - 401 与 403 的区别 |
| MEDIUM - 用户范围的缓存 |
Mental Model
思维模型
Server vs Client = different auth APIs:
- Server: from
await auth()(async!)@clerk/nextjs/server - Client: hook from
useAuth()(sync)@clerk/nextjs
Never mix them. Server Components use server imports, Client Components use hooks.
服务器端 vs 客户端 = 不同的身份验证API:
- 服务器端:来自的
@clerk/nextjs/server(异步!)await auth() - 客户端:来自的
@clerk/nextjs钩子(同步)useAuth()
切勿混用。服务器组件使用服务器端导入,客户端组件使用钩子。
Minimal Pattern
最简模式
typescript
// Server Component
import { auth } from '@clerk/nextjs/server'
export default async function Page() {
const { userId } = await auth() // MUST await!
if (!userId) return <p>Not signed in</p>
return <p>Hello {userId}</p>
}typescript
// Server Component
import { auth } from '@clerk/nextjs/server'
export default async function Page() {
const { userId } = await auth() // 必须使用await!
if (!userId) return <p>未登录</p>
return <p>你好 {userId}</p>
}Common Pitfalls
常见陷阱
| Symptom | Cause | Fix |
|---|---|---|
| Missing | |
| Auth not working on API routes | Missing matcher | Add `'/(api |
| Cache returns wrong user's data | Missing userId in key | Include |
| Mutations bypass auth | Unprotected Server Action | Check |
| Wrong HTTP error code | Confused 401/403 | 401 = not signed in, 403 = no permission |
| 症状 | 原因 | 修复方案 |
|---|---|---|
服务器组件中 | 缺少 | 使用 |
| API路由中身份验证不生效 | 缺少匹配规则 | 在中间件中添加`'/(api |
| 缓存返回错误用户的数据 | 缓存键中缺少userId | 在 |
| 突变操作绕过身份验证 | Server Action未受保护 | 在Action开头检查 |
| HTTP错误代码使用错误 | 混淆401/403 | 401 = 未登录,403 = 无权限 |
See Also
另请参阅
setup/managing-orgs/
setup/managing-orgs/