Next JS Authentication

Secure token storage (HttpOnly Cookies) and Middleware patterns. Use when this capability is needed.

tomevault-io Updated

File contents

Authentication & Token Management

Priority: P0 (CRITICAL)

Use HttpOnly Cookies for token storage. Never use LocalStorage.

Key Rules

  1. Storage: Use cookies().set() with httpOnly: true, secure: true, sameSite: 'lax'.
    • Reference: Auth Implementation (See "Setting Tokens").
  2. Access: Read tokens in Server Components via cookies().get().
    • Reference: Auth Implementation (See "Reading Tokens").
  3. Protection: Guard routes in middleware.ts before rendering.
    • Reference: Auth Implementation (See "Middleware Protection").

Anti-Pattern: LocalStorage

  • Security Risk: Vulnerable to XSS.
  • Performance Hit: Incompatible with Server Components (RSC). Forces client hydration and causes layout shift.

Converted and distributed by TomeVault — claim your Tome and manage your conversions.

tomevault-io/skills-registry/tree/main/ngxtm--devkit--authentication commit df9327ee4e

Frequently asked questions

npx skillmds@latest add tomevault-io/next-js-authentication