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

How It Works

Understanding the architecture behind next-api-layer helps you make the most of its features and troubleshoot effectively.

Architecture Overview#

The library consists of two main components working together:

LayerComponentResponsibilities
ProxycreateAuthProxyToken validation, auto refresh, guest tokens, CSRF, rate limiting
API ClientcreateApiClientXSS sanitization, i18n injection, method spoofing, auth handling

Request Flow:

Loading...

Request Flow#

When a request comes into your Next.js application, here's what happens:

1. Proxy Intercepts#

The createAuthProxy proxy intercepts every request:

TypeScript
Loading...

2. Token Validation#

The proxy checks for authentication tokens in cookies:

  1. User Token Check: First, it looks for the user token cookie
  2. Guest Token Fallback: If no user token, checks for guest token
  3. Validation: Validates the found token against your backend's /auth/me endpoint
  4. Caching: Valid tokens are cached to reduce backend calls

3. Auto Refresh#

If a token is expired but a refresh is possible:

  1. Calls your backend's /auth/refresh endpoint
  2. Updates the cookie with the new token
  3. Sets x-refreshed-token header for downstream use
  4. Continues the request with the new token

4. Route Protection#

Based on your access configuration:

TypeScript
Loading...
  • Protected Routes: Require authentication, redirect to /login if not authenticated
  • Auth Routes: Login/register pages, redirect away if already authenticated
  • Public Routes: Accessible by anyone

5. API Client Processing#

When your route handlers use the API client:

TypeScript
Loading...

The client:

  1. Retrieves the token (from refreshed header or cookies)
  2. Makes the request to your backend
  3. Sanitizes the response (XSS protection)
  4. Returns a Response object

Security Pipeline#

Every request passes through multiple security layers:

Rate Limiting#

TypeScript
Loading...

Protects against DoS attacks by limiting requests per IP.

CSRF Protection#

TypeScript
Loading...

Uses modern Fetch Metadata headers combined with signed double-submit cookies.

XSS Sanitization#

TypeScript
Loading...

All API responses are sanitized to prevent XSS attacks.

Audit Logging#

TypeScript
Loading...

Track authentication events for security monitoring.

Token Cascade#

Tokens are retrieved in this priority order:

  1. x-refreshed-token header: If proxy refreshed the token
  2. User token cookie: For authenticated users
  3. Guest token cookie: For anonymous sessions
TypeScript
Loading...

Composability#

You can extend the proxy behavior with hooks:

TypeScript
Loading...

Server Components Support#

For Server Components, use getServerUser:

TypeScript
Loading...

This reads the x-auth-user header set by the proxy, avoiding extra backend calls.