clerk-nuxt-patterns

작성자: clerk

Nuxt 3 인증 패턴 with @clerk/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

clerk의 다른 스킬

clerk-monorepo
clerk
Work effectively in the clerk/javascript SDK monorepo. Use when setting up the repo, building / testing / running a package, deciding which of the @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 상태 머신을 작성하고 사용합니다. 사용자가 createMachine으로 상태 머신을 작성하거나, 다단계 흐름을 모델링하거나, 머신을 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 — 미들웨어, SSR 페이지, 아일랜드 컴포넌트, API 라우트, 정적 렌더링 vs SSR 렌더링. 다음에서 트리거: astro clerk, clerk astro middleware,…
official
clerk-backend-api
clerk
Clerk 백엔드 REST API 탐색기 및 실행기. 태그를 탐색하고, 엔드포인트 스키마를 검사하며, 인증된 요청을 실행합니다. 사용자 목록 조회, 관리 시 사용하세요…
official
clerk-chrome-extension-patterns
clerk
Chrome 확장 프로그램 인증 with @clerk/chrome-extension -- 팝업/사이드패널 설정, 웹 앱을 통한 OAuth/SAML용 syncHost, 서비스 워커용 createClerkClient 및…
official
clerk-cli
clerk
Operate the Clerk CLI (`clerk` binary) for authentication, user/org/session management, impersonation, local webhook testing, deploy verification, instance…
official