clerk-setup
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAdding Clerk
添加Clerk身份验证
This skill sets up Clerk for authentication by following the official quickstart documentation.
本技能会按照官方快速入门文档的步骤,为项目搭建Clerk身份验证系统。
Quick Reference
快速参考
| Step | Action |
|---|---|
| 1. Detect framework | Check |
| 2. Fetch quickstart | Use WebFetch on the appropriate docs URL |
| 3. Follow instructions | Execute the steps from the official guide |
| 4. Get API keys | From dashboard.clerk.com |
| 步骤 | 操作 |
|---|---|
| 1. 检测框架 | 检查 |
| 2. 获取快速入门指南 | 使用WebFetch获取对应框架的文档URL |
| 3. 遵循操作说明 | 执行官方指南中的步骤 |
| 4. 获取API密钥 | 从dashboard.clerk.com获取 |
Framework Detection
框架检测
Check to identify the framework:
package.json| Dependency | Framework | Quickstart URL |
|---|---|---|
| Next.js | |
| Remix | |
| Astro | |
| Nuxt | |
| React Router | |
| TanStack Start | |
| React SPA | |
| Vue | |
| Express | |
| Fastify | |
| Expo | |
For other platforms:
- Chrome Extension:
https://clerk.com/docs/chrome-extension/getting-started/quickstart - Android:
https://clerk.com/docs/android/getting-started/quickstart - iOS:
https://clerk.com/docs/ios/getting-started/quickstart - Vanilla JavaScript:
https://clerk.com/docs/js-frontend/getting-started/quickstart
检查以识别项目框架:
package.json| 依赖项 | 框架 | 快速入门URL |
|---|---|---|
| Next.js | |
| Remix | |
| Astro | |
| Nuxt | |
| React Router | |
| TanStack Start | |
| React单页应用 | |
| Vue | |
| Express | |
| Fastify | |
| Expo | |
其他平台:
- Chrome扩展:
https://clerk.com/docs/chrome-extension/getting-started/quickstart - Android:
https://clerk.com/docs/android/getting-started/quickstart - iOS:
https://clerk.com/docs/ios/getting-started/quickstart - 原生JavaScript:
https://clerk.com/docs/js-frontend/getting-started/quickstart
Decision Tree
决策树
User Request: "Add Clerk" / "Add authentication"
│
├─ Read package.json
│
├─ Existing auth detected?
│ │
│ ├─ YES → Audit current auth → Create migration plan
│ │ → See "Migrating from Another Auth Provider"
│ │
│ └─ NO → Fresh install
│
├─ Identify framework from dependencies
│
├─ WebFetch the appropriate quickstart URL
│
└─ Follow the official instructions step-by-step用户请求:"添加Clerk" / "添加身份验证"
│
├─ 读取package.json
│
├─ 检测到已有身份验证系统?
│ │
│ ├─ 是 → 审核当前身份验证系统 → 制定迁移计划
│ │ → 查看"从其他身份验证提供商迁移"章节
│ │
│ └─ 否 → 全新安装
│
├─ 根据依赖项识别框架
│
├─ 使用WebFetch获取对应框架的快速入门URL
│
└─ 逐步遵循官方操作说明Setup Process
搭建流程
1. Detect the Framework
1. 检测框架
Read the project's and match dependencies to the table above.
package.json读取项目的,根据上述表格匹配对应的框架。
package.json2. Fetch the Quickstart Guide
2. 获取快速入门指南
Use WebFetch to retrieve the official quickstart for the detected framework:
WebFetch: https://clerk.com/docs/{framework}/getting-started/quickstart
Prompt: "Extract the complete setup instructions including all code snippets, file paths, and configuration steps."使用WebFetch获取对应框架的官方快速入门指南:
WebFetch: https://clerk.com/docs/{framework}/getting-started/quickstart
Prompt: "提取完整的搭建说明,包括所有代码片段、文件路径和配置步骤。"3. Follow the Instructions
3. 遵循操作说明
Execute each step from the quickstart guide:
- Install the required packages
- Set up environment variables
- Add the provider/middleware
- Create sign-in/sign-up routes if needed
- Test the integration
执行快速入门指南中的每一步:
- 安装所需的包
- 配置环境变量
- 添加Provider/中间件
- 如有需要,创建登录/注册路由
- 测试集成效果
4. Get API Keys
4. 获取API密钥
Two paths for development API keys:
Keyless (Automatic)
- On first SDK initialization, Clerk auto-generates dev keys and shows "Claim your application" popover
- No manual key setup required—keys are created and injected automatically
- Simplest path for new projects
Manual (Dashboard)
- Get keys from dashboard.clerk.com if Keyless doesn't trigger
- Publishable Key: Starts with or
pk_test_pk_live_ - Secret Key: Starts with or
sk_test_sk_live_ - Set as environment variables: and
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEYCLERK_SECRET_KEY
获取开发环境API密钥有两种方式:
无密钥模式(自动)
- 首次初始化SDK时,Clerk会自动生成开发密钥,并显示"Claim your application"弹窗
- 无需手动配置密钥——密钥会自动创建并注入
- 是新项目最简单的配置方式
手动模式(控制台)
- 如果无密钥模式未触发,可从dashboard.clerk.com获取密钥
- 可发布密钥:以或
pk_test_开头pk_live_ - 密钥:以或
sk_test_开头sk_live_ - 设置为环境变量:和
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEYCLERK_SECRET_KEY
Migrating from Another Auth Provider
从其他身份验证提供商迁移
If the project already has authentication, create a migration plan before replacing it.
如果项目已存在身份验证系统,在替换前需制定迁移计划。
Detect Existing Auth
检测已有身份验证系统
Check for existing auth libraries:
package.json- /
next-auth→ NextAuth/Auth.js@auth/core - → Supabase Auth
@supabase/supabase-js - /
firebase→ Firebase Authfirebase-admin - → AWS Cognito
@aws-amplify/auth - /
auth0→ Auth0@auth0/nextjs-auth0 - → Passport.js
passport - Custom JWT/session implementation
检查中的身份验证相关依赖:
package.json- /
next-auth→ NextAuth/Auth.js@auth/core - → Supabase身份验证
@supabase/supabase-js - /
firebase→ Firebase身份验证firebase-admin - → AWS Cognito
@aws-amplify/auth - /
auth0→ Auth0@auth0/nextjs-auth0 - → Passport.js
passport - 自定义JWT/会话实现
Migration Process
迁移流程
-
Audit current auth - Identify all auth touchpoints:
- Sign-in/sign-up pages
- Session/token handling
- Protected routes and middleware
- User data storage (database tables, external IDs)
- OAuth providers configured
-
Create migration plan - Consider:
- User data export - Export users and import via Clerk's Backend API
- Password hashes - Clerk can upgrade hashes to Bcrypt transparently
- External IDs - Store legacy user IDs as in Clerk
external_id - Session handling - Existing sessions will terminate on switch
-
Choose migration strategy:
- Big bang - Switch all users at once (simpler, requires maintenance window)
- Trickle migration - Run both systems temporarily (lower risk, higher complexity)
-
审核当前身份验证系统 - 识别所有身份验证相关触点:
- 登录/注册页面
- 会话/令牌处理
- 受保护路由和中间件
- 用户数据存储(数据库表、外部ID)
- 已配置的OAuth提供商
-
制定迁移计划 - 考虑以下事项:
- 用户数据导出 - 导出用户数据并通过Clerk的后端API导入
- 密码哈希 - Clerk可自动将哈希升级为Bcrypt格式
- 外部ID - 将旧用户ID存储为Clerk中的
external_id - 会话处理 - 切换系统后,现有会话将终止
-
选择迁移策略:
- 一次性迁移 - 同时为所有用户切换系统(实现简单,但需要维护窗口)
- 渐进式迁移 - 临时同时运行两个系统(风险较低,但复杂度更高)
Migration Reference
迁移参考文档
- Migration Overview: https://clerk.com/docs/guides/development/migrating/overview
Common Pitfalls
常见问题
| Level | Issue | Solution |
|---|---|---|
| CRITICAL | Missing | In Next.js 15+, |
| CRITICAL | Exposing | Never use secret key in client code; only |
| HIGH | Missing middleware matcher | Include API routes: `matcher: ['/((?!.\.. |
| HIGH | ClerkProvider not at root | Must wrap entire app in root layout/App component |
| HIGH | Auth routes not public | Allow |
| HIGH | Landing page requires auth | To keep "/" public, exclude it: `matcher: ['/((?!.\.. |
| MEDIUM | Wrong import path | Server code uses |
| 级别 | 问题 | 解决方案 |
|---|---|---|
| 严重 | 调用 | 在Next.js 15+中, |
| 严重 | 暴露 | 绝不要在客户端代码中使用密钥;只有 |
| 高 | 缺少中间件匹配规则 | 需包含API路由:`matcher: ['/((?!.\.. |
| 高 | ClerkProvider未在根组件中包裹 | 必须在根布局/App组件中包裹整个应用 |
| 高 | 身份验证路由未设为公开 | 在中间件配置中允许 |
| 高 | 首页要求身份验证 | 若要将"/"设为公开路由,需在匹配规则中排除:`matcher: ['/((?!.\.. |
| 中 | 导入路径错误 | 服务端代码使用 |
See Also
相关内容
- - Custom sign-in/up components
custom-flows/ - - Webhook → database sync
syncing-users/ - - B2B multi-tenant organizations
managing-orgs/ - - E2E testing setup
testing/ - - Advanced Next.js patterns
nextjs-patterns/
- - 自定义登录/注册组件
custom-flows/ - - Webhook → 数据库同步
syncing-users/ - - B2B多租户组织管理
managing-orgs/ - - 端到端测试搭建
testing/ - - 高级Next.js模式
nextjs-patterns/
Documentation
文档链接
- Quickstart Overview: https://clerk.com/docs/getting-started/quickstart/overview
- Migration Guide: https://clerk.com/docs/guides/development/migrating/overview
- Full Documentation: https://clerk.com/docs