nextjs-better-auth-session-guard

Detect Better Auth session invalidation in Next.js 16 App Router apps.

1|Updated Jan 23, 2026
One-click install
npx skills add https://github.com/Hankanman/claude-config --skill nextjs-better-auth-session-guard
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: nextjs-better-auth-session-guard
Source: https://github.com/Hankanman/claude-config/tree/main/config/skills/nextjs-better-auth-session-guard
Command: npx skills add https://github.com/Hankanman/claude-config --skill nextjs-better-auth-session-guard

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill ensures that session invalidation in Next.js 16 App Router apps using Better Auth is detected in real time on the client, preventing protected content from remaining visible after sign-out.

Core Features & Use Cases

  • Dual-layer authentication: server-side initial check plus client-side monitoring to detect session changes.
  • Immediate invalidation: redirects when the session becomes invalid, across tabs and after reloads.
  • E2E friendly: supports tests that verify session persistence and sign-out behavior.
  • Seamless UX: preserves return URLs and avoids unnecessary loading spinners.

Quick Start

Integrate a SessionGuard into your protected layout and configure the Better Auth client to poll session state (fetchOnWindowFocus, fetchOnReconnect, refetchInterval) for real-time invalidation.

Frequently Asked Questions about nextjs-better-auth-session-guard

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I detect Better Auth session invalidation in real time within a Next.js App Router?

Real-time session invalidation in Next.js requires a dual-layer approach combining a server-side initial check with a client-side SessionGuard component that monitors the Better Auth useSession hook for runtime changes.

Why does protected content remain visible after sign out in Next.js when using Better Auth?

Protected content remains visible after sign out when relying solely on server-side checks; adding a client-side SessionGuard using the Better Auth useSession hook ensures invalid sessions are detected across tabs and redirects occur immediately.

How to configure Better Auth client polling for session changes across tabs and reloads?

Configure Better Auth client polling for session changes by enabling fetchOnWindowFocus, fetchOnReconnect, and setting a configurable refetchInterval to ensure the client detects session invalidation across browser tabs and after page reloads.

Does this real-time session guard approach work with E2E tests for sign out behavior?

Yes, this real-time session guard approach works with E2E tests by supporting verifications of session persistence and sign out behavior, while preserving return URLs and avoiding unnecessary loading spinners during redirects.

When do I need a client-side SessionGuard component for Next.js authentication?

You need a client-side SessionGuard component for Next.js authentication when server-side initial checks are sufficient for loading but you must detect runtime session changes, such as sign-outs occurring in other tabs or after page reloads.