Next.jsMedium

What is NextAuth.js (Auth.js)?

Auth.js, originally NextAuth.js, is an open-source authentication library that adds OAuth providers, email magic links, credentials login, sessions (JWT or database) and adapters for many databases to Next.js. Since September 2025 it is maintained by the Better Auth team, who keep it patched but recommend Better Auth for new projects.

What it provides

  • Providers: Google, GitHub, Microsoft Entra ID, Apple and dozens more, plus email and credentials.
  • Sessions: encrypted JWT cookies by default, or database sessions through adapters (Prisma, Drizzle, MongoDB, Supabase).
  • Callbacks: jwt, session, signIn and authorized to add roles or block users.
  • Security defaults: CSRF tokens, PKCE and state checks for OAuth, secure cookies.

Versions

  • next-auth v4: the stable line, built around the Pages Router API (getServerSession).
  • Auth.js v5 (next-auth@5): App Router-first, with a single auth() function usable in Server Components, Route Handlers, Server Functions and Proxy. It has stayed in beta for a long time, though many teams run it in production.

Setup in brief (v5)

  1. Create auth.ts exporting { handlers, auth, signIn, signOut } from NextAuth({ providers }).
  2. Expose handlers at app/api/auth/[...nextauth]/route.ts.
  3. Set AUTH_SECRET and provider credentials in env vars.
  4. Call await auth() on the server, useSession() on the client.

Interview angle

Know the concepts (providers, callbacks, JWT vs database sessions) and the current landscape: Better Auth, Clerk and similar libraries are common choices in 2026. Mention that you still authorize inside your data layer, whatever library you use.

Code example

tsx
1// auth.ts
2import NextAuth from "next-auth";
3import GitHub from "next-auth/providers/github";
4import Google from "next-auth/providers/google";
5
6export const { handlers, auth, signIn, signOut } = NextAuth({
7 providers: [GitHub, Google], // read AUTH_GITHUB_ID / AUTH_GITHUB_SECRET etc. from env
8 callbacks: {
9 async jwt({ token, profile }) {
10 if (profile?.email?.endsWith("@acme.com")) token.role = "staff";
11 return token;
12 },
13 async session({ session, token }) {
14 session.user.role = (token.role as string) ?? "user";
15 return session;
16 },
17 },
18});
19
20// app/api/auth/[...nextauth]/route.ts
21// export const { GET, POST } = handlers;
22
23// app/dashboard/page.tsx
24// const session = await auth();
25// if (!session) redirect("/api/auth/signin");

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the Next.js set.

Open in study app