next-api-layerNext API Layer
DocumentationAPI ReferenceExamples
next-api-layerNext API Layer

Production-grade API layer for Next.js with external JWT backends.

Documentation

  • Introduction
  • Installation
  • Quick Start
  • API Reference

Resources

  • Examples
  • Proxy
  • API Client
  • AuthProvider

Community

  • GitHub
  • Issues
  • Discussions
  • Contact

© 2026 Next API Layer. All rights reserved.

Created by
Documentation

Getting Started

  • Introduction
  • Installation
  • Quick Start

Core Concepts

  • How It Works
  • Token Management
  • Guest Tokens

Configuration

  • Auth Proxy
  • Proxy Handler
  • API Client
  • Security
  • i18n Integration

Client Side

  • AuthProvider
  • useAuth Hook

API Reference

  • API Reference
  • Types

Examples

  • Examples
  • Authentication Patterns
  • Role-Based Access
  • API Routes
  • Forms
  • Data Fetching
Changelog

Getting Started

  • Introduction
  • Installation
  • Quick Start

Core Concepts

  • How It Works
  • Token Management
  • Guest Tokens

Configuration

  • Auth Proxy
  • Proxy Handler
  • API Client
  • Security
  • i18n Integration

Client Side

  • AuthProvider
  • useAuth Hook

API Reference

  • API Reference
  • Types

Examples

  • Examples
  • Authentication Patterns
  • Role-Based Access
  • API Routes
  • Forms
  • Data Fetching
Changelog

Token Management

next-api-layer handles JWT token management automatically, including validation, refresh, and caching.

Overview#

The library manages three key token operations:

  1. Validation: Checking if a token is valid
  2. Refresh: Automatically refreshing expired tokens
  3. Caching: Reducing backend calls with smart caching

Token Validation#

Every request triggers token validation through your backend's validate endpoint:

TypeScript
Loading...

Expected Response Format#

Your backend's /auth/me endpoint should return:

JSON
Loading...

Custom Response Parsing#

If your backend uses a different format, use responseMappers:

TypeScript
Loading...

Automatic Token Refresh#

When a token is invalid or expired, the library automatically attempts to refresh it:

TypeScript
Loading...

Refresh Flow#

  1. Token validation fails (expired/invalid)
  2. Library calls /auth/refresh with current token
  3. If successful, updates cookie with new token
  4. Sets x-refreshed-token header for the request
  5. Continues with the original request

Expected Refresh Response#

JSON
Loading...

Custom Refresh Parsing#

TypeScript
Loading...

Token Caching#

To minimize backend calls, validated tokens are cached:

TypeScript
Loading...

Cache Behavior#

  • Valid tokens are cached for 2 seconds by default
  • Cache is per-token (different users have separate entries)
  • Cache automatically cleans up expired entries
  • Pending validation requests are deduplicated

Deduplication#

If multiple simultaneous requests come in with the same token:

Loading...

This prevents thundering herd problems during high traffic.

Token Types#

The library supports different token types:

TypeScript
Loading...

Type Checking#

TypeScript
Loading...

Guest vs User Tokens#

FeatureGuest TokenUser Token
Created automaticallyYesNo (login required)
Cookie nameguestTokenuserToken
Typical TTL1 hour7 days
Can access protected routesNoYes

Cookie Configuration#

Control how tokens are stored:

TypeScript
Loading...

Security Best Practices#

  • Always use httpOnly: true to prevent XSS token theft
  • Use secure: true in production for HTTPS-only
  • sameSite: 'lax' provides good CSRF protection while allowing normal navigation
  • Set appropriate maxAge based on your security requirements

Accessing Tokens#

In Route Handlers#

TypeScript
Loading...

With API Client#

The API client handles token retrieval automatically:

TypeScript
Loading...

Token Expiration Handling#

Proxy Level#

The proxy handles expiration automatically:

  1. Detects expired token from validation response
  2. Attempts refresh
  3. If refresh fails, clears cookies and redirects to login (for protected routes)

Client Level#

The AuthProvider keeps client state in sync:

TypeScript
Loading...

Debugging#

Enable audit logging to track token operations:

TypeScript
Loading...

This helps identify:

  • Frequent refresh attempts (might indicate short token TTL)
  • Failed validations (might indicate backend issues)
  • Unauthorized access attempts