Expose the Auth.js catch-all route
Next.js App Router authentication
Auth.js · Function
handlers is the route-handler collection returned by NextAuth. Export it from the Auth.js configuration and re-export its GET and POST methods from the Next.js App Router authentication route.
handlers is a collection returned by NextAuth for Next.js. Import it into the App Router catch-all authentication route and re-export its HTTP methods so Auth.js can serve provider flows and its built-in REST endpoints.
const { handlers } = NextAuth(options)An object containing Next.js route handler methods, including GET and POST.
NextAuth returns handlers alongside other authentication helpers, allowing one configuration module to export the values the application needs.GET and POST methods from the route module.pages/ directory.// auth.ts
import NextAuth from "next-auth"
import GitHub from "next-auth/providers/github"
export const { handlers } = NextAuth({
providers: [GitHub],
})
// app/api/auth/[...nextauth]/route.ts
import { handlers } from "@/auth"
export const { GET, POST } = handlersCreate the Auth.js configuration, export its handlers, then expose those handlers as the GET and POST methods of the Next.js authentication route.
Next.js App Router authentication
OAuth or email provider integration
Client session and Auth.js REST requests
handlers used for in a Next.js Auth.js setup?handlers is returned when initializing NextAuth and contains the Next.js Route Handler methods. Re-exporting its GET and POST methods from app/api/auth/[...nextauth]/route.ts exposes Auth.js endpoints.
Follow-up: Why is the route path catch-all? It lets the handler receive the multiple authentication paths used by provider callbacks and Auth.js endpoints.
handlers to the application route?The configuration belongs in an auth.ts file, where NextAuth is initialized and handlers is exported. The route module then imports that export and maps its methods to GET and POST.
Follow-up: What other values can the same initialization export? Auth.js also exposes methods such as auth, signIn, and signOut, depending on the application’s needs.
The exported methods expose endpoints for OAuth and email providers as well as Auth.js REST endpoints such as the session endpoint. They are intended to be re-exported after Auth.js has been initialized.
Follow-up: How would you add custom application behavior? Keep the Auth.js handler route dedicated to these methods and implement unrelated application routes separately.
NextAuthauthsignInsignOutGETPOSTRoute Handler