Communityコーディング&開発github.com

clerk/clerk-nuxt-patterns

Clerk を使用した Nuxt 3 認証パターン - ミドルウェア、コンポーザブル、サーバー API ルート、SSR。トリガー:Nuxt auth、useAuth コンポーザブル、Nuxt の clerkMiddleware、サーバー API Clerk、Nuxt ルート保護。

clerk-nuxt-patterns とは?

このスキルは、Clerk 認証サービスを Nuxt 3 アプリケーションに統合するための包括的なパターンを提供します。具体的には、`@clerk/nuxt` パッケージを使用したミドルウェア、コンポーザブル(useAuth など)、サーバー API ルート、および SSR(サーバーサイドレンダリング)の設定方法をカバーします。Nuxt のルート保護、Clerk ミドルウェアの設定、サーバー API エンドポイントでの認証処理など、実際のプロジェクトで必要となる主要なユースケースに対応しています。このスキルは、Clerk と Nuxt 3 を組み合わせた認証機能を素早く実装したい開発者にとって有用であり、セキュアなユーザー認証と認可を効率的に構築できます。

対応~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/clerk/skills/tree/main/skills/clerk-nuxt-patterns

Installed? Explore more コーディング&開発 skills: steipete/bluebubbles, steipete/eightctl, steipete/blucli · View all 6 →

お気に入りのAIに質問する

このエージェントスキルを事前に読み込んだ状態で新しいチャットを開きます。

ドキュメント

clerk-nuxt-patterns は何をしますか?

Clerk を使用した Nuxt 3 認証パターン - ミドルウェア、コンポーザブル、サーバー API ルート、SSR。トリガー:Nuxt auth、useAuth コンポーザブル、Nuxt の clerkMiddleware、サーバー API Clerk、Nuxt ルート保護。

概要

このスキルは、@clerk/nuxt パッケージを用いて Nuxt 3 アプリケーションに Clerk 認証を統合するためのパターンを示します。以下の主要な機能をカバーします:

  • ミドルウェア: Nuxt のルートガードを使用した認証必須ルートの保護
  • コンポーザブル: useAuth などのコンポーザブルを使用したユーザー認証状態の管理
  • サーバー API ルート: サーバー API エンドポイントでの Clerk 認証の適用
  • SSR: サーバーサイドレンダリングにおける認証状態の取り扱い

前提条件

  • Nuxt 3 プロジェクトがセットアップされていること
  • @clerk/nuxt パッケージがインストールされていること(例:npm install @clerk/nuxt)
  • Clerk ダッシュボードで発行された API キー(NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY と CLERK_SECRET_KEY)が環境変数に設定されていること

使用方法

1. ミドルウェアによるルート保護

middleware/auth.global.ts または特定のルート用ミドルウェアファイルを作成します:

export default defineNuxtRouteMiddleware((to, from) => {
  const { isSignedIn } = useAuth();
  if (!isSignedIn.value && to.path !== '/sign-in') {
    return navigateTo('/sign-in');
  }
});

2. コンポーザブルの使用

useAuth コンポーザブルを使用して認証状態を取得します:

<script setup lang="ts">
const { isSignedIn, userId, sessionId } = useAuth();
</script>

<template>
  <div>
    <p v-if="isSignedIn">ログイン中: {{ userId }}</p>
    <p v-else>ログインしていません</p>
  </div>
</template>

3. サーバー API ルートでの認証

server/api/protected.ts のようなサーバー API ルートで Clerk 認証を行います:

export default defineEventHandler(async (event) => {
  const { auth } = event.context;
  if (!auth.userId) {
    throw createError({ statusCode: 401, message: 'Unauthorized' });
  }
  return { userId: auth.userId };
});

4. SSR での使用

plugins/clerk.client.ts と plugins/clerk.server.ts で適切に設定します(詳細は公式ドキュメント参照)。

注意点

  • 環境変数は必ず .env ファイルに設定し、公開リポジトリにコミットしないでください。
  • ミドルウェアの順序に注意し、グローバルミドルウェアとページ固有ミドルウェアを適切に使い分けてください。
  • サーバー API ルートでは event.context.auth を使用して認証情報にアクセスします。

参考リンク

Individual skills in this repo

This repo contains 19 individual skills — each has its own dedicated page.

clerk/clerk

Clerk authentication router. Use when user asks about adding authentication, setting up Clerk, custom sign-in flows, Swift or native iOS auth, native Android auth, Next.js patterns, React patterns, Vue patterns, Nuxt patterns, Astro patterns, TanStack Start patterns, Expo patterns, React Router patterns, Chrome Extension patterns, organizations, billing, subscriptions, payments, pricing, plans, seat-based pricing, feature entitlements, syncing users, or testing. Automatically routes to the specific skill based on their task.

clerk/clerk-android

Implement Clerk authentication for native Android apps using Kotlin and Jetpack Compose with clerk-android source-guided patterns. Use for prebuilt AuthView/UserButton or custom API-driven auth flows. Do not use for Expo or React Native projects.

clerk/clerk-astro-patterns

Astro patterns with Clerk — middleware, SSR pages, island components, API routes, static vs SSR rendering. Triggers on: astro clerk, clerk astro middleware, astro protected page, clerk island component, astro API route auth, clerk astro SSR.

clerk/clerk-backend-api

Clerk Backend REST API explorer and executor. Browse tags, inspect endpoint schemas, and execute authenticated requests. Use when listing users, managing organizations, or calling any Clerk API endpoint.

clerk/clerk-billing

Clerk Billing for subscription management - render Clerk's PricingTable and in-app checkout drawer, configure subscription plans, seat-limit plans for B2B, feature entitlements with has(), and billing webhooks. Use for SaaS monetization, plan gating, checkout flows, trials, invoicing, and subscription lifecycle management.

clerk/clerk-chrome-extension-patterns

Chrome Extension auth with @clerk/chrome-extension -- popup/sidepanel setup, syncHost for OAuth/SAML via web app, createClerkClient for service workers and headless extensions, stable CRX ID. Triggers on: Chrome extension auth, Plasmo clerk, popup sign-in, syncHost, background service worker token, createClerkClient, headless extension.

clerk/clerk-custom-ui

Custom authentication flows and component appearance - hooks (useSignIn, useSignUp), themes, colors, fonts, CSS. Use for custom sign-in/sign-up flows, appearance styling, visual customization, branding.

clerk/clerk-expo

Implement Clerk authentication for Expo and React Native apps using @clerk/expo source-guided patterns. Use for prebuilt AuthView/UserButton or custom hook-driven auth flows. Do not use for native iOS/Swift, native Android/Kotlin, or web framework projects.

clerk/clerk-expo-patterns

Expo / React Native patterns with Clerk — SecureStore token cache, OAuth deep linking, useAuth in native, Expo Router protected routes, push notifications with user context. Triggers on: expo clerk, clerk react native, SecureStore token cache, expo router auth, OAuth deep link clerk, mobile auth clerk.

clerk/clerk-nextjs-patterns

Advanced Next.js patterns - middleware, Server Actions, caching with Clerk.

clerk/clerk-orgs

Clerk Organizations for B2B SaaS - create multi-tenant apps with org switching, role-based access, verified domains, and enterprise SSO. Use for team workspaces, RBAC, org-based routing, member management.

clerk/clerk-react-patterns

React SPA auth patterns with @clerk/react for Vite/CRA - ClerkProvider setup, useAuth/useUser/useClerk hooks, React Router protected routes, custom sign-in flows. Triggers on: Vite Clerk setup, React Router auth, useAuth hook, protected route, custom sign-in form React.

clerk/clerk-react-router-patterns

React Router v7 patterns with Clerk — rootAuthLoader, getAuth in loaders, clerkMiddleware, protected routes, SSR user data, org switching. Triggers on: react-router auth, rootAuthLoader, getAuth loader, react-router protected route, loader authentication, SSR auth react-router.

clerk/clerk-setup

Add Clerk authentication to any project by following the official quickstart guides.

clerk/clerk-swift

Implement Clerk authentication for native Swift and iOS apps using ClerkKit and ClerkKitUI source-guided patterns. Use for prebuilt AuthView or custom native flows. Do not use for Expo or React Native projects.

clerk/clerk-tanstack-patterns

TanStack React Start auth patterns with @clerk/tanstack-react-start - createServerFn, beforeLoad guards, loaders, Vinxi server. Triggers on: TanStack auth, createServerFn clerk, beforeLoad protection, TanStack Start middleware.

clerk/clerk-testing

E2E testing for Clerk apps. Use with Playwright or Cypress for auth flow tests.

clerk/clerk-vue-patterns

Vue 3 patterns with Clerk — composables (useAuth, useUser, useClerk, useOrganization), Vue Router guards, Pinia auth store integration. Triggers on: vue clerk, useAuth vue, clerk composables, vue router clerk guard, pinia auth clerk. For Nuxt, use clerk-nuxt-patterns instead.

clerk/clerk-webhooks

Clerk webhooks for real-time events and data syncing. Verify with verifyWebhook from the framework-specific package. Handle user, session, organization, billing, and payment events. Build event-driven features like database sync, notifications, and integrations.

関連スキル