clerk-nuxt-patterns

โดย clerk

รูปแบบการตรวจสอบสิทธิ์ Nuxt 3 ด้วย @clerk/nuxt - middleware, composables, server API routes, SSR. ทำงานเมื่อ: Nuxt auth, useAuth composable, clerkMiddleware Nuxt,…

npx skills add https://github.com/clerk/skills --skill clerk-nuxt-patterns

Nuxt Patterns

What Do You Need?

TaskReference
Protect routes with middlewarereferences/nuxt-middleware.md
Auth in server API routes (Nitro)references/server-api-routes.md
useAuth / useUser in componentsreferences/composables.md
SSR-safe auth patternsreferences/ssr-auth.md

References

ReferenceDescription
references/nuxt-middleware.mdRoute protection, clerkMiddleware()
references/server-api-routes.mdNitro server route auth
references/composables.mduseAuth, useUser, useClerk
references/ssr-auth.mdSSR hydration, server vs client

Setup

npm install @clerk/nuxt

.env:

NUXT_PUBLIC_CLERK_PUBLISHABLE_KEY=pk_...
NUXT_CLERK_SECRET_KEY=sk_...

nuxt.config.ts:

export default defineNuxtConfig({
  modules: ['@clerk/nuxt'],
})

This single line auto-configures middleware, plugins, and component auto-imports.

Mental Model

@clerk/nuxt auto-imports all Clerk components and composables — no explicit imports needed in <script setup>.

  • Composables (useAuth, useUser) — client-side reactive, inside <script setup>
  • Server routes (clerkClient) — Nitro server routes, event.context.auth
  • Middleware (clerkMiddleware) — auto-registered, use auth().protect() to lock routes

Minimal Pattern

<!-- pages/dashboard.vue -->
<script setup lang="ts">
definePageMeta({ middleware: 'auth' })
const { userId } = useAuth()
</script>

<template>
  <Show when="signed-in">
    <p>Hello {{ userId }}</p>
  </Show>
</template>

definePageMeta({ middleware: 'auth' }) uses the built-in auth middleware from @clerk/nuxt.

Common Pitfalls

SymptomCauseFix
Composables return undefined on serveruseAuth is client-onlyUse event.context.auth in server routes
Route not protectedMissing middleware: 'auth' metaAdd definePageMeta({ middleware: 'auth' })
clerkClient not availableWrong import pathImport from @clerk/nuxt/server
Hydration mismatchRendering auth state before mountedWrap in <ClientOnly> or check isLoaded
Env vars not picked upWrong prefixNuxt requires NUXT_PUBLIC_ for public, NUXT_ for server

Org-Aware Pattern

<script setup lang="ts">
const { orgId, orgRole } = useAuth()
</script>

<template>
  <div v-if="orgId">
    <p>Org: {{ orgId }}</p>
    <p v-if="orgRole === 'org:admin'">Admin panel</p>
  </div>
  <div v-else>
    <OrganizationSwitcher />
  </div>
</template>

See Also

  • clerk-setup - Initial Clerk install
  • clerk-custom-ui - Custom flows & appearance
  • clerk-orgs - B2B organizations

Docs

Nuxt SDK

Skills เพิ่มเติมจาก clerk

clerk-monorepo
clerk
ทำงานอย่างมีประสิทธิภาพใน clerk/javascript SDK monorepo ใช้เมื่อตั้งค่า repo สร้าง / ทดสอบ / รันแพ็กเกจ ตัดสินใจว่า @clerk/* ตัวใด…
official
mosaic
clerk
Work on Mosaic UI: styling a component with slot recipes (`defineSlotRecipe` / `useRecipe` / slots / variants), or building a flow — authoring a state machine…
official
mosaic-machine
clerk
ผู้เขียนและใช้ Mosaic state machines ใช้เมื่อผู้ใช้กำลังเขียน state machine ด้วย createMachine, จำลองโฟลว์หลายขั้นตอน, เชื่อมต่อ machine กับ React…
official
clerk-android
clerk
Implement Clerk authentication for native Android apps using Kotlin and Jetpack Compose with clerk-android source-guided patterns. Use for prebuilt…
official
clerk-astro-patterns
clerk
รูปแบบ Astro กับ Clerk — middleware, หน้า SSR, คอมโพเนนต์ island, เส้นทาง API, การเรนเดอร์แบบ static เทียบกับ SSR. 触发เมื่อ: astro clerk, clerk astro middleware,…
official
clerk-backend-api
clerk
เครื่องมือสำรวจและดำเนินการ Clerk Backend REST API เรียกดูแท็ก ตรวจสอบสคีมาของเอนด์พอยต์ และดำเนินการคำขอที่ผ่านการรับรองความถูกต้อง ใช้เมื่อแสดงรายการผู้ใช้ จัดการ…
official
clerk-chrome-extension-patterns
clerk
การรับรองความถูกต้องของ Chrome Extension ด้วย @clerk/chrome-extension -- การตั้งค่า popup/sidepanel, syncHost สำหรับ OAuth/SAML ผ่านเว็บแอป, createClerkClient สำหรับ service workers และ…
official
clerk-cli
clerk
Operate the Clerk CLI (`clerk` binary) for authentication, user/org/session management, impersonation, local webhook testing, deploy verification, instance…
official