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,signInandauthorizedto 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 singleauth()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)
- Create
auth.tsexporting{ handlers, auth, signIn, signOut }fromNextAuth({ providers }). - Expose
handlersatapp/api/auth/[...nextauth]/route.ts. - Set
AUTH_SECRETand provider credentials in env vars. - 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.ts2import NextAuth from "next-auth";3import GitHub from "next-auth/providers/github";4import Google from "next-auth/providers/google";56export const { handlers, auth, signIn, signOut } = NextAuth({7 providers: [GitHub, Google], // read AUTH_GITHUB_ID / AUTH_GITHUB_SECRET etc. from env8 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});1920// app/api/auth/[...nextauth]/route.ts21// export const { GET, POST } = handlers;2223// app/dashboard/page.tsx24// 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.