Back to roadmaps authjs Course

Setting up Prisma Adapter with Auth.js

To save user accounts, profile details, and session tokens directly in your own SQL database, connect Auth.js to your Prisma setup using the official adapter.


1. Installing the Prisma Adapter

Install the prisma-adapter dependency package:

# Install the Prisma Adapter for Auth.js
npm install @auth/prisma-adapter

2. Linking the Adapter in configurations (auth.ts)

Import your local Prisma Client instance and bind it to the adapter property inside auth.ts:

// auth.ts
import NextAuth from "next-auth";
import GitHub from "next-auth/providers/github";
import { PrismaAdapter } from "@auth/prisma-adapter";
import { prisma } from "./lib/prisma";

export const { handlers, signIn, signOut, auth } = NextAuth({
  // Register the database adapter
  adapter: PrismaAdapter(prisma),
  providers: [
    GitHub,
  ],
  // When using database adapters, you can use the stateful database session strategy
  session: {
    strategy: "database",
  },
});

Note: Once the adapter is enabled, Auth.js will automatically create a user record in your local database when a user signs in via GitHub for the first time.

Published on Last updated: