1. Fundamentals and Version Choice
Understand NextAuth/Auth.js, authentication versus authorization, and the version used throughout this guide.
What NextAuth.js Does
NextAuth.js is the established name for the Next.js integration of Auth.js. It handles provider sign-in flows, callback endpoints, CSRF protections, cookies, and session retrieval. It does not automatically implement registration, password resets, your user database, roles, or application-specific permissions.
This tutorial's runnable main path targets NextAuth v4 with the Pages Router (pages/). It demonstrates Google OAuth and a Credentials provider that delegates password verification to a trusted backend. A later chapter explains v5/App Router differences.
Choose One Version and Router
Check your installed package with npm ls next-auth. The main examples use v4: pages/api/auth/[...nextauth].js, NextAuth(authOptions), getServerSession(req, res, authOptions), and callbackUrl. Auth.js v5 commonly uses auth.ts, exports handlers and auth, and uses auth() in server contexts. Do not mix snippets across major versions.
The current Auth.js documentation describes v5 and later in many places. Use the v4 docs for the runnable v4 implementation in this tutorial.
The Complete Authentication Flow
A provider verifies identity; callbacks apply policy and shape token/session data; the browser receives a protected session cookie; UI hooks read safe session data; server checks authorize access.