Generated banner for Clerk Nuxt Patterns
Generated logo for Clerk Nuxt Patterns

Clerk Nuxt Patterns

ClerkOfficialCreated by clerk

  • Skill
  • Tool integration
  • Software development
  • MIT
  • Low risk
  • Has scripts

From the author

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.
— the description by clerk

AutoGineer summary

Written by AutoGineer from the skill’s own files, not by the author

This skill provides a comprehensive set of patterns for integrating Clerk authentication into Nuxt 3 projects. It covers the essential building blocks including route protection via middleware, handling authentication state in components with composables, securing server-side API routes, and managing SSR-safe authentication flows. It serves as a reference for developers to implement secure, user-aware features in their Nuxt applications.

What it does

  • Configures Clerk authentication within the Nuxt 3 framework.
  • Provides patterns for protecting client-side routes using built-in middleware.
  • Demonstrates how to access authentication state and user data via auto-imported composables.
  • Explains how to secure Nitro server API routes using server-side context.
  • Offers guidance on handling SSR hydration and common authentication pitfalls.
  • Shows how to implement organization-aware authentication flows.

When to use it

  • When starting a new Nuxt 3 project that requires user authentication.
  • When migrating existing Nuxt applications to use Clerk for identity management.
  • When you need to protect specific routes or API endpoints based on user session status.
  • When building B2B applications requiring organization-based access control.

Advantages

  • Simplifies integration with auto-imported components and composables.
  • Provides standardized patterns for both client-side and server-side authentication.
  • Reduces boilerplate code for common tasks like route protection and user data retrieval.
  • Addresses specific Nuxt-related challenges like SSR hydration and environment variable configuration.

Usage examples

“How do I protect a dashboard page in my Nuxt 3 app using Clerk?”
The agent will explain how to add definePageMeta({ middleware: 'auth' }) to the page script to automatically redirect unauthenticated users.
“How can I access the current user's ID in a Nuxt component?”
The agent will show how to use the useAuth() composable to retrieve the userId and display it in the template.
“How do I secure a Nitro server route in Nuxt with Clerk?”
The agent will provide an example of using event.context.auth within a server route to verify the user's session.

Install

  1. Download the skill as a ZIP:

    https://mp-staging.autogineer.com/agent-skills/clerk/clerk-nuxt-patterns/download
  2. Unzip it into the cross-client skills folder of your project:

    mkdir -p .agents/skills && unzip clerk-nuxt-patterns.zip -d .agents/skills

Documentation by clerk

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

Files

evals/evals.json

{
  "skill_name": "clerk-nuxt-patterns",
  "evals": [
    {
      "id": 1,
      "prompt": "i have a nuxt 3 app with @clerk/nuxt installed. i need to protect my /dashboard route so only signed-in users can access it. unauthenticated users should be redirected to the sign-in page.",
      "expected_output": "Adds definePageMeta middleware auth guard to dashboard page, or creates a route middleware using clerkMiddleware",
      "scaffold": "nuxt-basic-auth",
      "expectations": [
        "Uses definePageMeta({ middleware: 'auth' }) on the dashboard page OR creates a named route middleware",
        "Handles redirect for unauthenticated users",
        "Does NOT use client-side only checks for route protection",
        "Imports are correct for @clerk/nuxt",
        "Does not break the existing app structure"
      ]
    },
    {
      "id": 2,
      "prompt": "i need to create a server API route in my nuxt app that returns the current user's data. the route should only work for authenticated users and return 401 for unauthenticated requests.",
      "expected_output": "Creates a Nitro server route using event.context.auth or clerkClient to get the current user",
      "scaffold": "nuxt-basic-auth",
      "expectations": [
        "Creates a file in server/api/ directory",
        "Accesses auth state via event.context.auth or imports clerkClient from @clerk/nuxt/server",
        "Returns 401 status for unauthenticated requests",
        "Returns user data for authenticated requests",
        "Uses defineEventHandler from Nitro"
      ]
    },
    {
      "id": 3,
      "prompt": "i want to show user profile information on my nuxt page - the user's name, email, and avatar. how do i access this data in my page component?",
      "expected_output": "Uses useUser() composable to access user data in the Vue component",
      "scaffold": "nuxt-basic-auth",
      "expectations": [
        "Uses useUser() composable (auto-imported by @clerk/nuxt)",
        "Accesses user.firstName, user.lastName, user.emailAddresses, or user.imageUrl",
        "Handles the loading state before rendering user data",
        "Does NOT make a server fetch for user data when composables suffice",
        "Component uses <script setup> syntax"
      ]
    },
    {
      "id": 4,
      "prompt": "my nuxt saas app needs organization support. users should be able to switch between their organizations in the header. show me how to add org switching and display org-scoped content on the dashboard.",
      "expected_output": "Adds OrganizationSwitcher component, reads orgId from useAuth() to scope dashboard content",
      "scaffold": "nuxt-basic-auth",
      "expectations": [
        "Adds <OrganizationSwitcher /> component to the header or layout",
        "Uses orgId from useAuth() composable to scope data",
        "Handles case where no org is selected (prompts user to select or create one)",
        "Does NOT remove existing user authentication logic",
        "Dashboard content is conditional on orgId being present"
      ]
    },
    {
      "id": 5,
      "prompt": "i want to replace the hosted sign-in page with a custom one inside my nuxt app. show me how to create a custom sign-in page using clerk components.",
      "expected_output": "Creates a pages/sign-in.vue page with <SignIn /> component and configures the sign-in URL",
      "scaffold": "nuxt-basic-auth",
      "expectations": [
        "Creates a sign-in page in the pages/ directory",
        "Renders the <SignIn /> component from @clerk/nuxt",
        "Configures NUXT_PUBLIC_CLERK_SIGN_IN_URL or signInUrl in nuxt.config.ts",
        "Does NOT implement a custom auth form from scratch",
        "Page is publicly accessible (no auth middleware on it)"
      ]
    },
    {
      "id": 6,
      "prompt": "i need to protect a nitro server route that handles payments. the route needs to verify the user is authenticated and get their userId to record the payment in my database.",
      "expected_output": "Server route using event.context.auth to get userId, returns 401 if not authenticated",
      "scaffold": "nuxt-basic-auth",
      "expectations": [
        "Uses event.context.auth.userId in a Nitro server route",
        "Returns createError({ statusCode: 401 }) or setResponseStatus(event, 401) for unauthenticated",
        "Extracts userId from auth context to use in database operation",
        "File is placed in server/api/ or server/routes/",
        "Does NOT re-verify the session token manually"
      ]
    },
    {
      "id": 7,
      "prompt": "i'm adding clerk to my nuxt 3 app. set up the module and protect the /dashboard route so unauthenticated users are redirected to /sign-in.",
      "expected_output": "Adds @clerk/nuxt to modules in nuxt.config.ts, creates Nuxt route middleware that checks auth and redirects",
      "scaffold": "nuxt-basic-auth",
      "expectations": [
        "Adds @clerk/nuxt to the modules array in nuxt.config.ts",
        "Creates a route middleware that checks authentication",
        "Uses useAuth composable inside the middleware",
        "Redirects unauthenticated users to /sign-in via navigateTo",
        "Applies the middleware to the /dashboard route"
      ]
    },
    {
      "id": 8,
      "prompt": "my nuxt app has a server API route /api/user-data that should only work for authenticated users. implement the server-side auth check.",
      "expected_output": "Uses getAuth from @clerk/nuxt/server in the event handler, throws 401 when userId is falsy",
      "scaffold": "nuxt-basic-auth",
      "expectations": [
        "Imports getAuth from @clerk/nuxt/server",
        "Calls getAuth(event) to get userId",
        "Throws createError({ statusCode: 401 }) when userId is falsy",
        "Uses userId in the data fetching logic",
        "Does NOT use client-side composables in the server route"
      ]
    },
    {
      "id": 9,
      "prompt": "i need to add a global nuxt server middleware that checks auth on all protected routes and redirects to /sign-in. use clerkMiddleware from @clerk/nuxt.",
      "expected_output": "Creates server/middleware/clerk.ts with clerkMiddleware that checks H3Event context for auth",
      "scaffold": "nuxt-basic-auth",
      "expectations": [
        "Creates server/middleware/clerk.ts",
        "Uses clerkMiddleware from @clerk/nuxt/server",
        "Exports the result as default",
        "Can access auth state via event.context.auth",
        "Does NOT create the middleware in middleware/ (that is for route middleware, not server middleware)"
      ]
    }
  ]
}

Requirements and compatibility

Works with: Claude Code, Claude apps, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, Kiro, OpenCode, Goose, Amp, Roo Code, JetBrains Junie, Factory Droid, Devin (ex-Windsurf), Mistral Vibe, OpenHands, Letta

“Works with” means the tool reads the open Agent Skills format; a skill written for one tool may use features others lack.

Needs: Declares allowed tools, Needs Node.js

Security

What our scanner noticed· Low risk

  • Scripts that your agent may run

This skill includes scripts that your agent may run. Read them before you use it.

A static scan is not a guarantee. Skills are third-party content: review a skill before you let an agent use it.

Attribution and licence

Created by
clerk
Publisher
Clerk (Official)
Source
clerk/skills · skills/clerk-nuxt-patterns · commit c5d2c0d
Licence
MIT License (redistribution allowed)
  • ATTRIBUTION.md (written by AutoGineer)
Synced

AutoGineer is not affiliated with or endorsed by clerk.

Download ZIP

The ZIP holds the skill’s files, its licence files and an ATTRIBUTION.md. Download clerk-nuxt-patterns.zip