Next.js 16 Authentication with NextAuth v5 (2026 Guide)

How to Add Authentication to Next.js 16

NextAuth v5 (Auth.js) is the standard authentication library for Next.js. Here is how to set it up.

Step 1: Install

npm install next-auth@beta @auth/prisma-adapter

Step 2: Configure Auth

// auth.ts
import NextAuth from 'next-auth';
import Google from 'next-auth/providers/google';
import GitHub from 'next-auth/providers/github';
import { PrismaAdapter } from '@auth/prisma-adapter';

export const { handlers, auth, signIn, signOut } = NextAuth({
  adapter: PrismaAdapter(prisma),
  providers: [Google, GitHub],
});

Step 3: Route Handler

1
2
3
// app/api/auth/[...nextauth]/route.ts
import { handlers } from '@/auth';
export const { GET, POST } = handlers;

Step 4: Middleware

// middleware.ts
export { auth as middleware } from '@/auth';

Step 5: Use in Components

1
2
3
4
5
6
7
// Server component
import { auth } from '@/auth';
export default async function Dashboard() {
  const session = await auth();
  if (!session) redirect('/login');
  return <h1>Welcome {session.user.name}</h1>;
}

Want This Already Built?

ShipKit includes complete NextAuth v5 setup with email, Google, and GitHub providers, plus Prisma database, Stripe payments, dark mode, landing page, and dashboard. 149 USD, one-time payment, unlimited projects.

Get it: rentry.co/devtools-store
Pay: ETH/USDC/USDT to 0xA18dC9fA96089d273421a57C18de0bf1FE9FaB52
Email: [email protected]

Edit

Pub: 13 May 2026 17:24 UTC

Views: 5