Create the Auth.js configuration and export its generated methods.
Application initialization
Auth.js · Function
NextAuth initializes Auth.js for a Next.js application from a NextAuthConfig and returns integration methods. Its result includes universal authentication access and route handlers.
Calling NextAuth with an authentication configuration initializes Auth.js for a Next.js application and returns framework integration methods. The returned auth method can be used across Middleware, Server Components, Route Handlers, and API Routes, while handlers connect the authentication endpoints.
NextAuth(config)| Name | Type | Description |
|---|---|---|
config | NextAuthConfig | Authentication configuration containing providers and other Auth.js options. |
A NextAuthResult containing methods such as auth and handlers for setting up and interacting with Auth.js in a Next.js application.
import NextAuth from "next-auth"
import GitHub from "next-auth/providers/github"
export const { handlers, auth } = NextAuth({
providers: [GitHub],
})Create the Auth.js configuration and export the generated handlers and universal auth method.
Application initialization
Server-side authentication
Authentication endpoints
NextAuth initializes Auth.js from a NextAuthConfig and returns a NextAuthResult. That result provides integration methods such as auth and handlers for use in a Next.js application.
Follow-up: How would you use the returned auth method? Use it in Middleware, Server Components, Route Handlers, or API Routes to interact with authentication.
The auth method is a universal method returned by NextAuth. After configuration, it can be used in Middleware, Server Components, Route Handlers under app/, and Edge or Node.js API Routes.
Follow-up: Why is this useful? A single exported method replaces framework-location-specific server authentication access patterns.
The handlers returned by NextAuth are used to set up the authentication route handlers in a Next.js application. The configuration also accepts providers, which define available authentication integrations.
Follow-up: What must be configured for a provider-based login? Add the provider to the providers array passed to NextAuth.
NextAuthResultauthhandlersNextAuthConfigproviders