Prisma Adapter - NextAuth.js
Open Source. Full Stack. Own Your Data.
Overview
This is the Prisma Adapter for next-auth
. This package can only be used in conjunction with the primary next-auth
package. It is not a standalone package.
You can find the Prisma schema in the docs at next-auth.js.org/adapters/prisma.
Getting Started
- Install
next-auth
and @next-auth/prisma-adapter
npm install next-auth @next-auth/prisma-adapter
- Add this adapter to your
pages/api/[...nextauth].js
next-auth configuration object.
import NextAuth from "next-auth"
import Providers from "next-auth/providers"
import { PrismaAdapter } from "@next-auth/prisma-adapter"
import * as Prisma from "@prisma/client"
const prisma = new Prisma.PrismaClient()
export default NextAuth({
providers: [
Providers.Google({
clientId: process.env.GOOGLE_ID,
clientSecret: process.env.GOOGLE_SECRET,
}),
],
adapter: PrismaAdapter(prisma)
...
})
Custom Fields
In order to add custom fields to the database, you must first add them to your Prisma schema file, for example.
model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
> google String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
accounts Account[]
sessions Session[]
}
When running prisma migrate
, Prisma will create the User table with the above schema in your specified underlying database.
Then, during the first sign-in NextAuth.js will return the simpler prototype user object, but after the first sign-in, NextAuth.js will return the full custom User object, including your custom field and userId, in callbacks such as sign-in.
Contributing
We're open to all community contributions! If you'd like to contribute in any way, please read our Contributing Guide.
License
ISC