NextAuth.js
Open-source auth for Next.js supporting 50+ OAuth providers, JWT sessions, and database adapters. Flexible but configuration-heavy. ⚠️ Commit frequency has dropped — worth monitoring.
Why NextAuth.js?
You want fully open-source, self-hosted auth
You need custom OAuth providers or SAML
You're cost-sensitive and don't want per-MAU pricing
Signal Breakdown
What drives the Trust Score
Download Trend
Last 12 months
Tradeoffs & Caveats
Know before you commitYou want pre-built UI — NextAuth has none
You need organizations/teams — that's all DIY
The low recent commit count concerns your team
Pricing
Free tier & paid plans
100% free, open-source
Free & open-source
Self-hosted, no SaaS fees ever
Alternative Tools
Other options worth considering
Often Used Together
Complementary tools that pair well with NextAuth.js
Learning Resources
Docs, videos, tutorials, and courses
Get Started
Repository and installation options
View on GitHub
github.com/nextauthjs/next-auth
npm install next-authQuick Start
Copy and adapt to get going fast
import NextAuth from 'next-auth';
import GitHub from 'next-auth/providers/github';
const handler = NextAuth({
providers: [
GitHub({
clientId: process.env.GITHUB_ID,
clientSecret: process.env.GITHUB_SECRET,
}),
],
});
export { handler as GET, handler as POST };Code Examples
Common usage patterns
Get session in a Server Component
Read the current session server-side
import { getServerSession } from 'next-auth';
import { authOptions } from '@/app/api/auth/[...nextauth]/route';
export default async function Dashboard() {
const session = await getServerSession(authOptions);
if (!session) redirect('/api/auth/signin');
return <h1>Hello, {session.user?.name}</h1>;
}Protect an API route
Return 401 when the user is not authenticated
import { getServerSession } from 'next-auth';
import { authOptions } from '@/app/api/auth/[...nextauth]/route';
import { NextResponse } from 'next/server';
export async function GET() {
const session = await getServerSession(authOptions);
if (!session) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
return NextResponse.json({ user: session.user });
}Database adapter (Prisma)
Persist sessions to a database with the Prisma adapter
import NextAuth from 'next-auth';
import { PrismaAdapter } from '@auth/prisma-adapter';
import { prisma } from '@/lib/prisma';
import GitHub from 'next-auth/providers/github';
export const authOptions = {
adapter: PrismaAdapter(prisma),
providers: [GitHub({ clientId: process.env.GITHUB_ID, clientSecret: process.env.GITHUB_SECRET })],
session: { strategy: 'database' },
};
const handler = NextAuth(authOptions);
export { handler as GET, handler as POST };Community Notes
Real experiences from developers who've used this tool