Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Better Auth · Function

toNextJsHandler

toNextJsHandler adapts a configured Better Auth instance for Next.js App Router route handlers. Export the returned HTTP methods from the authentication catch-all route.

How it works

toNextJsHandler adapts a configured Better Auth instance to Next.js App Router route handlers. Call it with your auth instance, then export the HTTP methods returned by the adapter from the catch-all route.

Syntax

toNextJsHandler(auth)

Parameters

NameTypeDescription
authBetter Auth instanceThe configured Better Auth instance whose authentication endpoints should be exposed.

Returns

An object containing Next.js route handlers, including GET and POST handlers for the authentication endpoint.

Key points

  • Mount the adapter in a catch-all authentication route.
  • The Next.js-specific adapter is imported from the better-auth/next-js entry point.
  • Destructure and export the HTTP handlers produced from the configured auth instance.
  • Configure the Better Auth instance before passing it to the adapter.

Common mistakes

  • This adapter is for the Next.js App Router; Pages Router applications should use toNodeHandler instead.
  • Although the route path can be customized, the documentation recommends retaining the standard catch-all path.

Example: Mount Better Auth in a Next.js App Router route

import { auth } from "@/lib/auth";
import { toNextJsHandler } from "better-auth/next-js";

export const { GET, POST } = toNextJsHandler(auth);

Place this in the App Router catch-all route at app/api/auth/[...all]/route.ts.

When to use it

Expose Better Auth endpoints

Next.js App Router

Export additional authentication methods

Plugin endpoints requiring several verbs

Interview questions

QWhat does toNextJsHandler do?

It converts a Better Auth instance into Next.js App Router handlers. The returned methods are exported from the catch-all route so Next.js can process authentication requests.

Follow-up: How would you expose additional HTTP methods for a plugin such as SCIM? Export those methods from the object returned by the adapter, for example GET, POST, PUT, PATCH, and DELETE.

QHow is it mounted in a Next.js App Router project?

Import the function from better-auth/next-js, pass the configured auth instance, and export the returned GET and POST handlers from app/api/auth/[...all]/route.ts.

Follow-up: Why is a catch-all route used? It lets the Better Auth endpoint receive requests for the framework's authentication API paths under the configured base path.

QWhat should be used with the Pages Router instead?

The function is intended for the App Router route-handler model. For the Pages Router, the documentation specifies toNodeHandler and requires disabling body parsing in the route configuration.

Follow-up: What configuration detail matters for Pages Router requests? Set bodyParser to false so the Node handler can process the request body itself.

  • toNodeHandler
  • Better Auth instance
  • Next.js App Router
  • GET
  • POST

Source: Next. Js 13-15.1. X (edge.... Checked 2026-10-15.