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

AuthProvider

The AuthProvider component manages authentication state on the client side using SWR for data fetching with automatic revalidation.

Setup#

Wrap your app with the AuthProvider:

TSX
Loading...
TSX
Loading...

Configuration#

userEndpoint#

The API endpoint to fetch user data.

TSX
Loading...

Default: /api/auth/me

loginEndpoint#

The API endpoint for login requests.

TSX
Loading...

Default: /api/auth/login

registerEndpoint#

The API endpoint for registration requests.

TSX
Loading...

Default: /api/auth/register

logoutEndpoint#

The API endpoint for logout requests.

TSX
Loading...

Default: /api/auth/logout

logoutRedirect#

Where to redirect after logout.

TSX
Loading...

swrConfig#

Custom SWR configuration.

TSX
Loading...

isGuestFn#

Custom function to determine if user is a guest.

TSX
Loading...

parseResponse#

Custom function to parse API response and extract user data.

TSX
Loading...

onLogin#

Callback when user logs in successfully.

TSX
Loading...

onLogout#

Callback when user logs out.

TSX
Loading...

onError#

Handle authentication errors.

TSX
Loading...

initialUser#

Provide initial user data (from server-side rendering).

TSX
Loading...

This prevents the loading flash on initial page load.

Full Example#

TSX
Loading...

Authentication Flow#

  1. Initial Load

    • If initialUser is provided, use it immediately
    • Otherwise, fetch from userEndpoint
    • isLoading is true during fetch
  2. Revalidation

    • On window focus (if enabled)
    • On network reconnect (if enabled)
    • On interval (if configured)
    • After mutations
  3. State Changes

    • onLogin/onLogout callbacks are called
    • Components using useAuth re-render

Backend API Endpoint#

Your backend endpoint should return user data in this format:

JSON
Loading...

Or for guests:

JSON
Loading...

Protected Routes#

Use with the useAuth hook for protected pages:

TSX
Loading...

TypeScript#

Custom User Type#

TSX
Loading...

AuthState Type#

TypeScript
Loading...

Peer Dependencies#

AuthProvider requires SWR as a peer dependency:

Terminal
Loading...

If SWR is not installed, an error will be thrown at runtime.