NextAuth + Prisma Integration Security

To secure NextAuth + Prisma integration, five things matter. Use the PrismaAdapter so sessions are stored in your database. Put user.id into the session callback, since your database queries need it. Check auth() in every API route and Server Component. Set a strong NEXTAUTH_SECRET (32+ characters), and configure your OAuth callback URLs properly. Get those right and sessions stay in your database, with user identity verified on the server.

Setup Time2-3 hours

TL;DR

The Prisma adapter makes NextAuth store sessions in your database. From there, add the user ID to the session in the session callback and check auth() in every API route. Don't skip NEXTAUTH_SECRET. For sensitive apps, database sessions are the safer pick over JWTs.

Auth Configuration NextAuth Prisma

lib/auth.ts
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 { handlers, auth, signIn, signOut } = NextAuth({
  adapter: PrismaAdapter(prisma),
  providers: [
    GitHub({
      clientId: process.env.GITHUB_ID!,
      clientSecret: process.env.GITHUB_SECRET!,
    }),
  ],
  callbacks: {
    session: ({ session, user }) => ({
      ...session,
      user: {
        ...session.user,
        id: user.id,  // Include user ID in session
      },
    }),
  },
})

Protected API Route

app/api/posts/route.ts
import { auth } from '@/lib/auth'
import { NextResponse } from 'next/server'
import { prisma } from '@/lib/prisma'

export async function POST(req: Request) {
  const session = await auth()

  if (!session?.user?.id) {
    return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
  }

  const body = await req.json()

  const post = await prisma.post.create({
    data: {
      title: body.title,
      authorId: session.user.id,  // Verified user ID from session
    },
  })

  return NextResponse.json(post)
}

export async function GET() {
  const session = await auth()

  if (!session?.user?.id) {
    return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
  }

  const posts = await prisma.post.findMany({
    where: { authorId: session.user.id },
  })

  return NextResponse.json(posts)
}

Protected Server Component

app/dashboard/page.tsx
import { auth } from '@/lib/auth'
import { redirect } from 'next/navigation'
import { prisma } from '@/lib/prisma'

export default async function Dashboard() {
  const session = await auth()

  if (!session?.user?.id) {
    redirect('/api/auth/signin')
  }

  const posts = await prisma.post.findMany({
    where: { authorId: session.user.id },
  })

  return (
    <div>
      <h1>Welcome, {session.user.name}</h1>
      {posts.map(post => <div key={post.id}>{post.title}</div>)}
    </div>
  )
}

Always include user ID in session callback. NextAuth leaves the database user ID out of the session by default. Add it in the callback so your Prisma queries can filter by the signed-in user.

Security Checklist

Pre-Launch Checklist

Check Your NextAuth Integration

Scan for authentication security issues.

Security Blueprints

NextAuth + Prisma Integration Security