mcpField
C
clerk-nuxt-patterns

clerk-nuxt-patterns

github.com/midudevDevelopment7.0k57 installsUpdated 1w ago
Repository

About clerk-nuxt-patterns

clerk-nuxt-patterns is a skill for developers integrating Clerk into Nuxt projects, covering route protection, Nitro server routes, composables, and SSR authentication. It describes module auto-configuration and the different use of auth state on the server and client.

Key features

  1. 01Protect Nuxt routes
  2. 02Handle auth in Nitro routes
  3. 03Use authentication composables
  4. 04Explain SSR and client auth patterns
  5. 05Describe module auto-configuration

Use cases

  1. 01Restrict access to Nuxt page routes
  2. 02Read auth details in Nitro API routes
  3. 03Use user and auth state in components
  4. 04Troubleshoot SSR hydration or environment variable issues

Install

Copy the command and run it in a terminal.

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

After installing, type/clerk-nuxt-patternsin Claude Code to confirm it loaded.

Bundled files

References and scripts installed along with SKILL.md.

evals/
evals.json
references/
composables.md
nuxt-middleware.md
server-api-routes.md
ssr-auth.md
templates/nuxt-basic-auth/
app.vue
nuxt.config.ts
package.json

Details

From the project README

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

Comments

Markdown supported