首页/AI 前端工程/clerk-nextjs-patterns
C

clerk-nextjs-patterns

by @clerkv
4.5(32)

专注于Clerk与Next.js集成模式,利用AI技能增强开发效率,简化用户认证与管理流程。

clerk-authnextjsauthenticationfrontend-developmentuser-managementGitHub
安装方式
npx skills add clerk/skills --skill clerk-nextjs-patterns
compare_arrows

Before / After 效果对比

1
使用前

在 Next.js 应用中手动集成认证功能,需要处理路由保护、会话管理和用户状态同步等复杂逻辑,耗时且易引入安全漏洞。

使用后

遵循 `clerk-nextjs-patterns` 提供的最佳实践,快速、安全地集成 Clerk 认证服务,简化开发,提升系统安全性。

SKILL.md

clerk-nextjs-patterns

Next.js Patterns

Version: Check package.json for the SDK version — see clerk skill for the version table. Core 2 differences are noted inline with > **Core 2 ONLY (skip if current SDK):** callouts.

For basic setup, see setup/.

Impact Levels

  • CRITICAL - Breaking bugs, security holes

  • HIGH - Common mistakes

  • MEDIUM - Optimization

References

Reference Impact

references/server-vs-client.md CRITICAL - await auth() vs hooks

references/middleware-strategies.md HIGH - Public-first vs protected-first, proxy.ts (Next.js <=15: middleware.ts)

references/server-actions.md HIGH - Protect mutations

references/api-routes.md HIGH - 401 vs 403

references/caching-auth.md MEDIUM - User-scoped caching

Mental Model

Server vs Client = different auth APIs:

  • Server: await auth() from @clerk/nextjs/server (async!)

  • Client: useAuth() hook from @clerk/nextjs (sync)

Never mix them. Server Components use server imports, Client Components use hooks.

Key properties from auth():

  • isAuthenticated — boolean, replaces the !!userId pattern

  • sessionStatus'active' | 'pending', for detecting incomplete session tasks

  • userId, orgId, orgSlug, has(), protect() — unchanged

Core 2 ONLY (skip if current SDK): isAuthenticated and sessionStatus are not available. Check !!userId instead.

Minimal Pattern

// Server Component
import { auth } from '@clerk/nextjs/server'

export default async function Page() {
  const { isAuthenticated, userId } = await auth()  // MUST await!
  if (!isAuthenticated) return <p>Not signed in</p>
  return <p>Hello {userId}</p>
}

Core 2 ONLY (skip if current SDK): isAuthenticated is not available. Use if (!userId) instead.

Conditional Rendering with <Show>

For client-side conditional rendering based on auth state:

import { Show } from '@clerk/nextjs'

<Show when="signed-in" fallback={<p>Please sign in</p>}>
  <Dashboard />
</Show>

Core 2 ONLY (skip if current SDK): Use <SignedIn> and <SignedOut> components instead of <Show>. See custom-ui/core-3/show-component.md for the full migration table.

Common Pitfalls

Symptom Cause Fix

undefined userId in Server Component Missing await await auth() not auth()

Auth not working on API routes Missing matcher Add `'/(api

Cache returns wrong user's data Missing userId in key Include userId in unstable_cache key

Mutations bypass auth Unprotected Server Action Check auth() at start of action

Wrong HTTP error code Confused 401/403 401 = not signed in, 403 = no permission

See Also

  • setup/

  • orgs/

Docs

Next.js SDK Weekly Installs4.3KRepositoryclerk/skillsGitHub Stars27First SeenJan 30, 2026Security AuditsGen Agent Trust HubPassSocketPassSnykPassInstalled oncursor3.8Kcodex2.2Kopencode2.2Kgithub-copilot2.2Kgemini-cli2.2Kamp2.1K

用户评价 (0)

发表评价

效果
易用性
文档
兼容性

暂无评价

统计数据

安装量12.4K
评分4.5 / 5.0
版本
更新日期2026年5月23日
对比案例1 组

用户评分

4.5(32)
5
19%
4
50%
3
28%
2
3%
1
0%

为此 Skill 评分

0.0

兼容平台

🔧Claude Code
🔧OpenClaw
🔧OpenCode
🔧Codex
🔧Gemini CLI
🔧GitHub Copilot
🔧Amp
🔧Kimi CLI

时间线

创建2026年3月17日
最后更新2026年5月23日