JWT vs database sessions, OAuth and credentials providers, auth() in Server Components and Actions, middleware-based route protection, RBAC, and the new forbidden()/unauthorized() auth interrupt system.
P-4 — Authentication with Auth.js (NextAuth v5)
Who this is for: Practitioners building applications that require user identity — login, session management, route protection, and role-based access. Auth.js v5 (formerly NextAuth) was rebuilt from scratch for the App Router and Next.js 15. If you've used NextAuth v4 before, almost everything changed. If you're starting fresh, this is the current standard.
What Changed in v5
NextAuth v4 was designed for the Pages Router. It worked in the App Router but with friction — getServerSession() had to be called with the same config object everywhere, the session wasn't naturally available in Server Components, and middleware integration was awkward.
Auth.js v5 was rewritten for the App Router with three design goals:
- A single
auth()call that works everywhere — Server Components, Route Handlers, Server Actions, and Middleware — without passing the config object around. - First-class edge runtime support. The middleware-based session check runs at the CDN edge with near-zero latency, not in a full Node.js runtime.
- Framework agnostic core. Auth.js v5 is built on a shared, framework-agnostic core (
@auth/core), with a separate package per framework:next-authfor Next.js,@auth/sveltekitfor SvelteKit,@auth/expressfor Express, and so on. Next.js's own integration is thenext-authpackage itself, not a re-export of some other Next.js-specific package.
The migration from v4 to v5 is significant — different import paths, different config structure, different session API. This module covers v5 exclusively.
Installation and Setup
Auth.js v5 uses the @beta tag as of late 2025 — it's stable and used in production, but the version tag reflects active development. Check npm show next-auth dist-tags for the current recommendation.
Create the core auth config:
The four exports are the entire Auth.js API surface:
handlers— theGETandPOSTRoute Handlers for OAuth callbacks and API endpointsauth— the session retrieval function (works everywhere)signIn— programmatic sign-in (Server Actions)signOut— programmatic sign-out (Server Actions)
The Route Handler
Auth.js needs a [...nextauth] catch-all route to handle OAuth redirects, callbacks, and the sign-in/sign-out API:
That's the entire file. handlers contains the fully configured GET and POST functions.
Reading the Session
The auth() function retrieves the current session. It works identically in every context:
Server Component:
Server Action:
Route Handler:
Sign in to keep reading
The rest of this module is free — sign in with Google to unlock it and track your progress.
Sign in & RegisterDiscussion
0Join the discussion