All skills
clerk avatar

/clerk-nuxt-patterns

@ca5e365 official
by clerkclerk/skills83 stars
5

Nuxt 3 auth patterns with @clerk/nuxt - middleware, composables, server API routes, SSR. Triggers on: Nuxt auth, useAuth composable, clerkMiddleware Nuxt, server API Clerk, Nuxt route protection.

Use this Skill: https://skilld.dev/gh/clerk/skills/clerk-nuxt-patterns

This session only. Nothing lands on disk.

SKILL.md

≈54 tokens always: the name and description. ≈710 when used: this file. ≈3.5k more on demand in 6 files.

Nuxt Patterns

What Do You Need?

Task Reference
Protect routes with middleware references/nuxt-middleware.md
Auth in server API routes (Nitro) references/server-api-routes.md
useAuth / useUser in components references/composables.md
SSR-safe auth patterns references/ssr-auth.md

References

Reference Description
references/nuxt-middleware.md Route protection, clerkMiddleware()
references/server-api-routes.md Nitro server route auth
references/composables.md useAuth, useUser, useClerk
references/ssr-auth.md SSR 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

Symptom Cause Fix
Composables return undefined on server useAuth is client-only Use event.context.auth in server routes
Route not protected Missing middleware: 'auth' meta Add definePageMeta({ middleware: 'auth' })
clerkClient not available Wrong import path Import from @clerk/nuxt/server
Hydration mismatch Rendering auth state before mounted Wrap in <ClientOnly> or check isLoaded
Env vars not picked up Wrong prefix Nuxt 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

Source: SKILL.md on GitHub

No alerts16d4 checks · Risk SAFE
  • Gen Agent Trust Hub16d

    The skill provides legitimate Nuxt 3 authentication patterns using the official Clerk SDK. All code snippets, configurations, and dependencies follow established best practices for secure application development.

  • Socket16d

    No alerts

  • Snyk16d

    Risk: LOW · No issues

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at ca5e365. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 20 hours ago.

Activeupdated 6 months ago
What it can do
Network
metadata
{
  "author": "clerk",
  "version": "1.0.0"
}
All 1 allowed tools
WebFetch
  • Nuxt
  • Auth
  • clerk
  • authentication
  • middleware
  • ssr
  • composables
  • nitro

README badge

README badge for clerk/skills/clerk-nuxt-patterns

Provides Nuxt 3 patterns for Clerk authentication, covering route protection with middleware, server API routes via Nitro, and client-side composables like useAuth and useUser. Includes SSR-safe patterns and organization-aware auth flows using @clerk/nuxt.

Generated from the current SKILL.md.

Does this skill work with Nuxt 3?
Yes. The skill covers Nuxt 3 patterns specifically, including auto-imports, composables, and the Nitro server runtime.
How do I protect routes from unauthenticated users?
Add `definePageMeta({ middleware: 'auth' })` to your page component. The `@clerk/nuxt` module auto-registers the auth middleware.
Can I use useAuth in server API routes?
No. useAuth is client-only. Use `event.context.auth` in Nitro server routes instead, or import `clerkClient` from `@clerk/nuxt/server`.
What causes hydration mismatches with auth state?
Rendering auth state before the component mounts. Fix this by wrapping sensitive content in `<ClientOnly>` or checking `isLoaded` before rendering.
How do I set up environment variables?
Use `NUXT_PUBLIC_CLERK_PUBLISHABLE_KEY` for the public key and `NUXT_CLERK_SECRET_KEY` for the secret key in your `.env` file.

Generated from the current SKILL.md. These answers refresh after source changes.