jpskill.com
🛠️ 開発・MCP コミュニティ

next-js-better-auth-integration

Next.js App Router環境で、より優れた認証機能を提供するBetter Authをスムーズに連携させ、セキュアなWebアプリケーション開発を効率化するSkill。

📜 元の英語説明(参考)

A conceptual skill for integrating Better Auth with Next.js App Router

🇯🇵 日本人クリエイター向け解説

一言でいうと

Next.js App Router環境で、より優れた認証機能を提供するBetter Authをスムーズに連携させ、セキュアなWebアプリケーション開発を効率化するSkill。

※ jpskill.com 編集部が日本のビジネス現場向けに補足した解説です。Skill本体の挙動とは独立した参考情報です。

⚡ おすすめ: コマンド1行でインストール(60秒)

下記のコマンドをコピーしてターミナル(Mac/Linux)または PowerShell(Windows)に貼り付けてください。 ダウンロード → 解凍 → 配置まで全自動。

🍎 Mac / 🐧 Linux
mkdir -p ~/.claude/skills && cd ~/.claude/skills && curl -L -o next-js-better-auth-integration.zip https://jpskill.com/download/16928.zip && unzip -o next-js-better-auth-integration.zip && rm next-js-better-auth-integration.zip
🪟 Windows (PowerShell)
$d = "$env:USERPROFILE\.claude\skills"; ni -Force -ItemType Directory $d | Out-Null; iwr https://jpskill.com/download/16928.zip -OutFile "$d\next-js-better-auth-integration.zip"; Expand-Archive "$d\next-js-better-auth-integration.zip" -DestinationPath $d -Force; ri "$d\next-js-better-auth-integration.zip"

完了後、Claude Code を再起動 → 普通に「動画プロンプト作って」のように話しかけるだけで自動発動します。

💾 手動でダウンロードしたい(コマンドが難しい人向け)
  1. 1. 下の青いボタンを押して next-js-better-auth-integration.zip をダウンロード
  2. 2. ZIPファイルをダブルクリックで解凍 → next-js-better-auth-integration フォルダができる
  3. 3. そのフォルダを C:\Users\あなたの名前\.claude\skills\(Win)または ~/.claude/skills/(Mac)へ移動
  4. 4. Claude Code を再起動

⚠️ ダウンロード・利用は自己責任でお願いします。当サイトは内容・動作・安全性について責任を負いません。

🎯 このSkillでできること

下記の説明文を読むと、このSkillがあなたに何をしてくれるかが分かります。Claudeにこの分野の依頼をすると、自動で発動します。

📦 インストール方法 (3ステップ)

  1. 1. 上の「ダウンロード」ボタンを押して .skill ファイルを取得
  2. 2. ファイル名の拡張子を .skill から .zip に変えて展開(macは自動展開可)
  3. 3. 展開してできたフォルダを、ホームフォルダの .claude/skills/ に置く
    • · macOS / Linux: ~/.claude/skills/
    • · Windows: %USERPROFILE%\.claude\skills\

Claude Code を再起動すれば完了。「このSkillを使って…」と話しかけなくても、関連する依頼で自動的に呼び出されます。

詳しい使い方ガイドを見る →
最終更新
2026-05-18
取得日時
2026-05-18
同梱ファイル
1

📖 Skill本文(日本語訳)

※ 原文(英語/中国語)を Gemini で日本語化したものです。Claude 自身は原文を読みます。誤訳がある場合は原文をご確認ください。

Next.js Better Auth Integration Skill

この Skill を使用するべき時

Better Auth を使用して App Router アーキテクチャで Next.js アプリケーションに認証を実装する必要がある場合は、この概念的な Skill を使用してください。この Skill は以下の場合に適しています。

  • Next.js アプリケーションへのユーザー認証の追加
  • Better Auth を使用した安全なセッション管理の実装
  • JWT ベースの認証フローの有効化
  • Next.js での保護されたルートとページの作成
  • アプリケーション全体でのユーザーセッションの管理
  • ソーシャルログイン機能の実装

この Skill は、以下の場合には使用しないでください。

  • Pages Router を使用するアプリケーション (_app.js を代わりに使用してください)
  • ユーザー認証を必要としないアプリケーション
  • ユーザーインタラクションのない単純な静的サイト
  • Better Auth と競合するカスタム認証要件を持つアプリケーション

前提条件

  • App Router が有効になっている Next.js 13+
  • Better Auth パッケージがインストールされ、構成されていること
  • Next.js ミドルウェアとサーバーコンポーネントの理解
  • 認証の概念とセッション管理の知識
  • 認証シークレットを安全に管理するための環境

概念的な実装フレームワーク

Better Auth 初期化機能

  • 適切なプロバイダーと設定で Better Auth を構成する
  • 認証データベースまたはアダプター構成を設定する
  • ユーザーモデルと認証オプションを定義する
  • フロントエンドで使用するための認証クライアントを初期化する
  • 認証コールバックとカスタマイズを構成する

JWT トークン有効化機能

  • JWT トークンの生成と検証の設定を構成する
  • トークンの有効期限と更新メカニズムを設定する
  • トークンのペイロード構造とクレームを定義する
  • 安全なトークンストレージと送信を有効にする
  • トークン署名と検証アルゴリズムを構成する

セッション管理のためのアプリラッピング機能

  • Better Auth プロバイダーで Next.js アプリケーションをラップする
  • クライアントコンポーネントのセッションコンテキストを構成する
  • サーバーコンポーネントでサーバー側のセッションアクセスを設定する
  • アプリケーションルート全体でセッションの永続性を確立する
  • クライアントとサーバー間のセッション同期を有効にする

Hook 使用機能

  • Hook を介して認証状態へのアクセスを提供する
  • コンポーネントでのユーザーセッションデータ取得を有効にする
  • リアルタイムでの認証ステータスチェックをサポートする
  • カスタム認証フローとコールバックを許可する
  • ログアウトとセッション管理機能を有効にする

保護されたルート実装機能

  • アプリケーションルートを保護するためのミドルウェアを作成する
  • サーバー側の認証チェックを実装する
  • クライアント側のセッション検証を有効にする
  • 許可されていないアクセスシナリオを適切に処理する
  • 認証ステータスに基づいてユーザーをリダイレクトする

期待される入力/出力

入力要件:

  1. Better Auth 構成:

    • データベース接続設定
    • 認証プロバイダーの構成
    • JWT 設定とシークレット
    • UI と動作のカスタマイズオプション
    • コールバック URL とリダイレクト設定
  2. Next.js アプリ構造:

    • App Router ディレクトリ構造 (/app)
    • ミドルウェア構成 (middleware.ts)
    • 認証コンテキストが必要なレイアウトファイル
    • 認証保護が必要なページ

出力形式:

  1. 初期化された認証システム:

    • 構成済みの Better Auth インスタンス
    • 使用可能な認証コンテキスト
    • 適切に設定されたセッション管理
    • 動作する JWT トークンシステム
  2. 認証されたコンポーネントの状態:

    • コンポーネントで利用可能なユーザーセッションデータ
    • 認証ステータス (ログイン/ログアウト)
    • 認証時のユーザープロファイル情報
    • セッショントークンと更新メカニズム
  3. 保護されたルートの応答:

    • 認証されたユーザーに対する HTTP 200 OK
    • 認証されていないユーザーに対する HTTP 302/307 リダイレクト
    • 認証失敗に対する適切なエラー処理
    • アプリケーション全体で一貫したセッション状態
  4. Hook の結果:

    • ユーザー認証ステータス
    • 利用可能な場合のセッションデータ
    • 認証チェック中のローディング状態
    • 認証失敗のエラーステート

統合パターン

App Router 統合

  • ルートレイアウトを構成して認証コンテキストを提供する
  • ルート保護のためのミドルウェアを実装する
  • サーバー側のセッションアクセスにサーバーコンポーネントを使用する
  • インタラクティブな認証 UI にクライアントコンポーネントを活用する

セッション管理

  • サーバーコンポーネントでのサーバー側のセッション処理
  • クライアントコンポーネントでのクライアント側のセッション同期
  • コンポーネント間のセッション状態の一貫性
  • 適切なセッションのクリーンアップと無効化

認証フロー

  • サインインおよびサインアッププロセスの管理
  • ソーシャル認証プロバイダーの統合
  • パスワードのリセットとアカウントの回復
  • 多要素認証 (サポートされている場合)

セキュリティに関する考慮事項

  1. トークンセキュリティ: 安全な JWT トークンのストレージと送信
  2. セッション管理: 適切なセッションの無効化と更新
  3. CSRF 保護: フォームの CSRF 保護を実装する
  4. 安全な Cookie: セッションに安全な HTTP-only Cookie を使用する
  5. 入力検証: すべての認証入力を検証する
  6. レート制限: 認証エンドポイントのレート制限を実装する
  7. シークレット管理: 認証シークレットを安全に保管する

パフォーマンスへの影響

  • ミドルウェアでのセッションルックアップのパフォーマンスを最適化する
  • セッションデータのキャッシュ戦略を検討する
  • 各リクエストの認証オーバーヘッドを最小限に抑える
  • 効率的なトークン検証メカニズム
  • 可能な場合は認証コンテキストの遅延ロード

エラー処理と検証

  • 認証の初期化の失敗を処理する
  • セッションの有効期限切れのシナリオを管理する
  • 認証の失敗に対する適切なフィードバックを提供する
  • 認証状態の一貫性を検証する
  • 認証チェック中のネットワーク障害を処理する

テストに関する考慮事項

  • サーバーコンポーネントで認証フローをテストする
  • リクエスト間のセッションの永続性を検証する
  • 保護されたルートアクセス制御を検証する
  • JWT トークンの生成と検証をテストする
  • ソーシャルログインプロバイダーの統合を検証する
  • テスト
📜 原文 SKILL.md(Claudeが読む英語/中国語)を展開

Next.js Better Auth Integration Skill

When to Use This Skill

Use this conceptual skill when you need to implement authentication in a Next.js application using the App Router architecture with Better Auth. This skill is appropriate for:

  • Adding user authentication to Next.js applications
  • Implementing secure session management with Better Auth
  • Enabling JWT-based authentication flows
  • Creating protected routes and pages in Next.js
  • Managing user sessions across the application
  • Implementing social login capabilities

This skill should NOT be used for:

  • Applications using the Pages Router (use _app.js instead)
  • Applications that don't require user authentication
  • Simple static sites without user interaction
  • Applications with custom authentication requirements that conflict with Better Auth

Prerequisites

  • Next.js 13+ with App Router enabled
  • Better Auth package installed and configured
  • Understanding of Next.js middleware and server components
  • Knowledge of authentication concepts and session management
  • Environment for managing authentication secrets securely

Conceptual Implementation Framework

Better Auth Initialization Capability

  • Configure Better Auth with appropriate providers and settings
  • Set up authentication database or adapter configuration
  • Define user model and authentication options
  • Initialize authentication client for frontend usage
  • Configure authentication callbacks and customizations

JWT Token Enablement Capability

  • Configure JWT token generation and validation settings
  • Set token expiration and refresh mechanisms
  • Define token payload structure and claims
  • Enable secure token storage and transmission
  • Configure token signing and verification algorithms

App Wrapping for Session Management Capability

  • Wrap the Next.js application with Better Auth provider
  • Configure session context for client components
  • Set up server-side session access in server components
  • Establish session persistence across application routes
  • Enable session synchronization between client and server

Hook Usage Capability

  • Provide access to authentication state via hooks
  • Enable user session data retrieval in components
  • Support authentication status checking in real-time
  • Allow for custom authentication flows and callbacks
  • Enable logout and session management functionality

Protected Route Implementation Capability

  • Create middleware for protecting application routes
  • Implement server-side authentication checks
  • Enable client-side session verification
  • Handle unauthorized access scenarios appropriately
  • Redirect users based on authentication status

Expected Input/Output

Input Requirements:

  1. Better Auth Configuration:

    • Database connection settings
    • Authentication providers configuration
    • JWT settings and secrets
    • Customization options for UI and behavior
    • Callback URLs and redirect settings
  2. Next.js App Structure:

    • App Router directory structure (/app)
    • Middleware configuration (middleware.ts)
    • Layout files that need authentication context
    • Pages requiring authentication protection

Output Formats:

  1. Initialized Authentication System:

    • Configured Better Auth instance
    • Ready-to-use authentication context
    • Properly set up session management
    • Working JWT token system
  2. Authenticated Component State:

    • User session data available in components
    • Authentication status (logged in/out)
    • User profile information when authenticated
    • Session tokens and refresh mechanisms
  3. Protected Route Responses:

    • HTTP 200 OK for authenticated users
    • HTTP 302/307 redirects for unauthenticated users
    • Proper error handling for authentication failures
    • Consistent session state across the application
  4. Hook Results:

    • User authentication status
    • Session data when available
    • Loading states during authentication checks
    • Error states for authentication failures

Integration Patterns

App Router Integration

  • Configure root layout to provide authentication context
  • Implement middleware for route protection
  • Use server components for server-side session access
  • Leverage client components for interactive authentication UI

Session Management

  • Server-side session handling in server components
  • Client-side session synchronization in client components
  • Cross-component session state consistency
  • Proper session cleanup and invalidation

Authentication Flows

  • Sign-in and sign-up process management
  • Social authentication provider integration
  • Password reset and account recovery
  • Multi-factor authentication (if supported)

Security Considerations

  1. Token Security: Secure JWT token storage and transmission
  2. Session Management: Proper session invalidation and refresh
  3. CSRF Protection: Implement CSRF protection for forms
  4. Secure Cookies: Use secure, HTTP-only cookies for sessions
  5. Input Validation: Validate all authentication inputs
  6. Rate Limiting: Implement rate limiting for authentication endpoints
  7. Secret Management: Securely store authentication secrets

Performance Implications

  • Optimize session lookup performance in middleware
  • Consider caching strategies for session data
  • Minimize authentication overhead on each request
  • Efficient token validation mechanisms
  • Lazy loading of authentication context when possible

Error Handling and Validation

  • Handle authentication initialization failures
  • Manage session expiration scenarios
  • Provide appropriate feedback for failed authentication
  • Validate authentication state consistency
  • Handle network failures during authentication checks

Testing Considerations

  • Test authentication flow in server components
  • Verify session persistence across requests
  • Validate protected route access controls
  • Test JWT token generation and validation
  • Verify social login provider integration
  • Test authentication edge cases and error states