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

Security

next-api-layer includes a comprehensive security system with multiple layers of protection. This page covers all security features and their configuration.

Overview#

The security system provides:

  • CSRF Protection - Prevents cross-site request forgery attacks
  • Rate Limiting - Protects against brute force and DDoS attacks
  • XSS Sanitization - Cleans malicious content from responses
  • Audit Logging - Tracks security events for compliance and debugging

CSRF Protection#

Cross-Site Request Forgery (CSRF) protection prevents malicious sites from making requests on behalf of authenticated users.

Configuration#

TypeScript
Loading...

Strategies#

fetch-metadata

Uses Sec-Fetch-* headers (modern browsers only).

TypeScript
Loading...

Validates:

  • Sec-Fetch-Site: Must be same-origin or same-site
  • Sec-Fetch-Mode: Checked for navigation vs. API requests

Pros: No token management needed Cons: Not supported in older browsers

double-submit

Classic double-submit cookie pattern.

TypeScript
Loading...

How it works:

  1. Server sets a CSRF cookie with a random token
  2. Client must send the same token in a header
  3. Server validates that cookie value matches header value

Client-side usage:

TypeScript
Loading...

both

Combines both strategies for maximum compatibility.

TypeScript
Loading...

Validates:

  1. First checks Sec-Fetch-* headers (if available)
  2. Then falls back to double-submit validation

Recommended for production.

Trust Same-Site#

TypeScript
Loading...

When enabled, requests with Sec-Fetch-Site: same-origin skip CSRF validation.

Ignore Methods#

TypeScript
Loading...

Safe methods that don't need CSRF protection.

Rate Limiting#

Protects your API from abuse by limiting the number of requests per time window.

Configuration#

TypeScript
Loading...

Options#

windowMs

Time window in milliseconds.

TypeScript
Loading...

maxRequests

Maximum requests allowed per window.

TypeScript
Loading...

keyFn

Function to generate rate limit key. Defaults to IP address.

TypeScript
Loading...

skipRoutes

Routes to exclude from rate limiting.

TypeScript
Loading...

onRateLimited

Custom response when rate limited.

TypeScript
Loading...

Headers#

Rate limit information is included in response headers:

Loading...

XSS Sanitization#

The API client automatically sanitizes response data to prevent XSS attacks.

Configuration#

TypeScript
Loading...

How It Works#

All string values in API responses are sanitized:

TypeScript
Loading...

Allowed Tags#

Specify which HTML tags are safe:

TypeScript
Loading...

Skip Fields#

Fields that should not be sanitized (use with caution):

TypeScript
Loading...

Custom Sanitizer#

TypeScript
Loading...

Audit Logging#

Track security-relevant events for compliance, debugging, and monitoring.

Configuration#

TypeScript
Loading...

Event Types#

EventDescription
auth:successUser successfully authenticated
auth:failAuthentication failed
auth:refreshToken was refreshed
auth:guestGuest token was created
access:deniedAccess to protected route denied
csrf:failCSRF validation failed
rateLimit:exceededRate limit exceeded
errorAn error occurred

Event Structure#

TypeScript
Loading...

Logger Examples#

Console Logger

TypeScript
Loading...

Database Logger

TypeScript
Loading...

External Service

TypeScript
Loading...

Security Best Practices#

Cookie Configuration#

TypeScript
Loading...

Environment-Specific Settings#

TypeScript
Loading...

Protect Sensitive Routes#

TypeScript
Loading...

Rate Limit Sensitive Endpoints#

TypeScript
Loading...

Security Headers#

The proxy automatically sets security headers:

Loading...

To customize:

TypeScript
Loading...