| name | next-auth |
| description | NextAuth.js / Auth.js authentication library for Next.js — providers, sessions, JWT, database adapters, middleware protection |
| layer | domain |
| category | authentication |
| triggers | ["next-auth","nextauth","authjs","auth.js","getServerSession","useSession"] |
| inputs | ["Authentication provider requirements","Session management strategy","Route protection rules"] |
| outputs | ["NextAuth configuration and route handlers","Provider setup (OAuth, credentials, magic link)","Session access patterns (server/client)","Middleware-based route protection"] |
| linksTo | ["nextjs","authentication","react","prisma"] |
| linkedFrom | [] |
| preferredNextSkills | ["nextjs","prisma","authentication"] |
| fallbackSkills | ["better-auth","authentication"] |
| riskLevel | medium |
| memoryReadPolicy | selective |
| memoryWritePolicy | none |
| sideEffects | [] |
NextAuth.js / Auth.js Patterns
Purpose
Provide expert guidance on NextAuth.js (Auth.js v5) for Next.js authentication, including OAuth providers, credentials auth, JWT/session strategies, database adapters, middleware protection, role-based access, and production security patterns.
Core Patterns
1. Auth.js v5 Setup (Next.js App Router)
npm install next-auth@beta @auth/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';
import Google from 'next-auth/providers/google';
import Credentials from 'next-auth/providers/credentials';
import { compare } from 'bcryptjs';
import type { DefaultSession } from 'next-auth';
declare module 'next-auth' {
interface Session {
user: {
id: string;
role: 'ADMIN' | | ;
} & [];
}
{
: | | ;
}
}
{ handlers, auth, signIn, signOut } = ({
: (prisma),
: { : },
: {
: ,
: ,
: ,
},
: [
({
: process..!,
: process..!,
}),
({
: process..!,
: process..!,
}),
({
: ,
: {
: { : , : },
: { : , : },
},
() {
(!credentials?. || !credentials?.) ;
user = prisma..({
: { : credentials. },
: { : , : , : , : , : , : },
});
(!user?.) ;
isValid = (credentials. , user.);
(!isValid) ;
{ : user., : user., : user., : user., : user. };
},
}),
],
: {
() {
(user) {
token. = user.;
token. = user.;
}
(trigger === && session) {
token. = session..;
token. = session..;
}
token;
},
() {
session.. = token. ;
session.. = token. | | ;
session;
},
() {
isLoggedIn = !!auth?.;
isOnDashboard = nextUrl..();
isOnAdmin = nextUrl..();
(isOnAdmin) {
auth?.?. === ;
}
(isOnDashboard) {
isLoggedIn;
}
;
},
},
: {
() {
.();
},
},
});