clerk-setup

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Adding Clerk

添加Clerk身份验证

This skill sets up Clerk for authentication by following the official quickstart documentation.
本技能会按照官方快速入门文档的步骤,为项目搭建Clerk身份验证系统。

Quick Reference

快速参考

StepAction
1. Detect frameworkCheck
package.json
dependencies
2. Fetch quickstartUse WebFetch on the appropriate docs URL
3. Follow instructionsExecute the steps from the official guide
4. Get API keysFrom dashboard.clerk.com
步骤操作
1. 检测框架检查
package.json
中的依赖项
2. 获取快速入门指南使用WebFetch获取对应框架的文档URL
3. 遵循操作说明执行官方指南中的步骤
4. 获取API密钥dashboard.clerk.com获取

Framework Detection

框架检测

Check
package.json
to identify the framework:
DependencyFrameworkQuickstart URL
next
Next.js
https://clerk.com/docs/nextjs/getting-started/quickstart
@remix-run/react
Remix
https://clerk.com/docs/remix/getting-started/quickstart
astro
Astro
https://clerk.com/docs/astro/getting-started/quickstart
nuxt
Nuxt
https://clerk.com/docs/nuxt/getting-started/quickstart
react-router
React Router
https://clerk.com/docs/react-router/getting-started/quickstart
@tanstack/react-start
TanStack Start
https://clerk.com/docs/tanstack-react-start/getting-started/quickstart
react
(no framework)
React SPA
https://clerk.com/docs/react/getting-started/quickstart
vue
Vue
https://clerk.com/docs/vue/getting-started/quickstart
express
Express
https://clerk.com/docs/expressjs/getting-started/quickstart
fastify
Fastify
https://clerk.com/docs/fastify/getting-started/quickstart
expo
Expo
https://clerk.com/docs/expo/getting-started/quickstart
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
Next.js
https://clerk.com/docs/nextjs/getting-started/quickstart
@remix-run/react
Remix
https://clerk.com/docs/remix/getting-started/quickstart
astro
Astro
https://clerk.com/docs/astro/getting-started/quickstart
nuxt
Nuxt
https://clerk.com/docs/nuxt/getting-started/quickstart
react-router
React Router
https://clerk.com/docs/react-router/getting-started/quickstart
@tanstack/react-start
TanStack Start
https://clerk.com/docs/tanstack-react-start/getting-started/quickstart
react
(无框架)
React单页应用
https://clerk.com/docs/react/getting-started/quickstart
vue
Vue
https://clerk.com/docs/vue/getting-started/quickstart
express
Express
https://clerk.com/docs/expressjs/getting-started/quickstart
fastify
Fastify
https://clerk.com/docs/fastify/getting-started/quickstart
expo
Expo
https://clerk.com/docs/expo/getting-started/quickstart
其他平台:
  • 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
package.json
and match dependencies to the table above.
读取项目的
package.json
,根据上述表格匹配对应的框架。

2. 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
    pk_test_
    or
    pk_live_
  • Secret Key: Starts with
    sk_test_
    or
    sk_live_
  • Set as environment variables:
    NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY
    and
    CLERK_SECRET_KEY
获取开发环境API密钥有两种方式:
无密钥模式(自动)
  • 首次初始化SDK时,Clerk会自动生成开发密钥,并显示"Claim your application"弹窗
  • 无需手动配置密钥——密钥会自动创建并注入
  • 是新项目最简单的配置方式
手动模式(控制台)
  • 如果无密钥模式未触发,可从dashboard.clerk.com获取密钥
  • 可发布密钥:以
    pk_test_
    pk_live_
    开头
  • 密钥:以
    sk_test_
    sk_live_
    开头
  • 设置为环境变量:
    NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY
    CLERK_SECRET_KEY

Migrating from Another Auth Provider

从其他身份验证提供商迁移

If the project already has authentication, create a migration plan before replacing it.
如果项目已存在身份验证系统,在替换前需制定迁移计划。

Detect Existing Auth

检测已有身份验证系统

Check
package.json
for existing auth libraries:
  • next-auth
    /
    @auth/core
    → NextAuth/Auth.js
  • @supabase/supabase-js
    → Supabase Auth
  • firebase
    /
    firebase-admin
    → Firebase Auth
  • @aws-amplify/auth
    → AWS Cognito
  • auth0
    /
    @auth0/nextjs-auth0
    → Auth0
  • passport
    → Passport.js
  • Custom JWT/session implementation
检查
package.json
中的身份验证相关依赖:
  • next-auth
    /
    @auth/core
    → NextAuth/Auth.js
  • @supabase/supabase-js
    → Supabase身份验证
  • firebase
    /
    firebase-admin
    → Firebase身份验证
  • @aws-amplify/auth
    → AWS Cognito
  • auth0
    /
    @auth0/nextjs-auth0
    → Auth0
  • passport
    → Passport.js
  • 自定义JWT/会话实现

Migration Process

迁移流程

  1. 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
  2. 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
      external_id
      in Clerk
    • Session handling - Existing sessions will terminate on switch
  3. Choose migration strategy:
    • Big bang - Switch all users at once (simpler, requires maintenance window)
    • Trickle migration - Run both systems temporarily (lower risk, higher complexity)
  1. 审核当前身份验证系统 - 识别所有身份验证相关触点:
    • 登录/注册页面
    • 会话/令牌处理
    • 受保护路由和中间件
    • 用户数据存储(数据库表、外部ID)
    • 已配置的OAuth提供商
  2. 制定迁移计划 - 考虑以下事项:
    • 用户数据导出 - 导出用户数据并通过Clerk的后端API导入
    • 密码哈希 - Clerk可自动将哈希升级为Bcrypt格式
    • 外部ID - 将旧用户ID存储为Clerk中的
      external_id
    • 会话处理 - 切换系统后,现有会话将终止
  3. 选择迁移策略
    • 一次性迁移 - 同时为所有用户切换系统(实现简单,但需要维护窗口)
    • 渐进式迁移 - 临时同时运行两个系统(风险较低,但复杂度更高)

Migration Reference

迁移参考文档

Common Pitfalls

常见问题

LevelIssueSolution
CRITICALMissing
await
on
auth()
In Next.js 15+,
auth()
is async:
const { userId } = await auth()
CRITICALExposing
CLERK_SECRET_KEY
Never use secret key in client code; only
NEXT_PUBLIC_*
keys are safe
HIGHMissing middleware matcherInclude API routes: `matcher: ['/((?!.\..
HIGHClerkProvider not at rootMust wrap entire app in root layout/App component
HIGHAuth routes not publicAllow
/sign-in
,
/sign-up
in middleware config
HIGHLanding page requires authTo keep "/" public, exclude it: `matcher: ['/((?!.\..
MEDIUMWrong import pathServer code uses
@clerk/nextjs/server
, client uses
@clerk/nextjs
级别问题解决方案
严重调用
auth()
时缺少
await
在Next.js 15+中,
auth()
是异步函数:
const { userId } = await auth()
严重暴露
CLERK_SECRET_KEY
绝不要在客户端代码中使用密钥;只有
NEXT_PUBLIC_*
开头的密钥可安全用于客户端
缺少中间件匹配规则需包含API路由:`matcher: ['/((?!.\..
ClerkProvider未在根组件中包裹必须在根布局/App组件中包裹整个应用
身份验证路由未设为公开在中间件配置中允许
/sign-in
/sign-up
路由访问
首页要求身份验证若要将"/"设为公开路由,需在匹配规则中排除:`matcher: ['/((?!.\..
导入路径错误服务端代码使用
@clerk/nextjs/server
,客户端代码使用
@clerk/nextjs

See Also

相关内容

  • custom-flows/
    - Custom sign-in/up components
  • syncing-users/
    - Webhook → database sync
  • managing-orgs/
    - B2B multi-tenant organizations
  • testing/
    - E2E testing setup
  • nextjs-patterns/
    - Advanced Next.js patterns
  • custom-flows/
    - 自定义登录/注册组件
  • syncing-users/
    - Webhook → 数据库同步
  • managing-orgs/
    - B2B多租户组织管理
  • testing/
    - 端到端测试搭建
  • nextjs-patterns/
    - 高级Next.js模式

Documentation

文档链接