Skip to content

Instantly share code, notes, and snippets.

@jclem
Last active March 25, 2022 13:59
Show Gist options
  • Save jclem/14d9ba9384c05a2df035619fbec8fcdc to your computer and use it in GitHub Desktop.
Save jclem/14d9ba9384c05a2df035619fbec8fcdc to your computer and use it in GitHub Desktop.
import {PrismaAdapter} from '@next-auth/prisma-adapter'
import {PrismaClient} from '@prisma/client'
import NextAuth from 'next-auth'
import {Adapter, AdapterUser} from 'next-auth/adapters'
import {Provider} from 'next-auth/providers'
import GitHubProvider from 'next-auth/providers/github'
function PrismaAdapterAugmentation(
prisma: PrismaClient
): Pick<Adapter, 'createUser' | 'linkAccount'> {
return {
async createUser(user) {
const {emailVerified: _, ...data} = user
return prisma.user.create({data}) as unknown as Promise<AdapterUser>
},
async linkAccount(account) {
const {access_token: _, token_type: _2, scope: _3, ...data} = account
await prisma.account.create({data})
}
}
}
function FixedPrismaAdapter(client: PrismaClient): Adapter {
const prismaAdapter = PrismaAdapter(client)
return {...prismaAdapter, ...PrismaAdapterAugmentation(client)}
}
const providers: Provider[] = [
GitHubProvider({
clientId: process.env.GITHUB_CLIENT_ID,
clientSecret: process.env.GITHUB_CLIENT_SECRET,
checks: ['state']
})
]
const prisma = new PrismaClient()
export default NextAuth({
adapter: FixedPrismaAdapter(prisma),
providers,
session: {
strategy: 'jwt',
maxAge: 30 * 24 * 60 * 60,
updateAge: 24 * 60 * 60
}
})
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "sqlite"
url = env("DATABASE_URL")
}
model User {
id String @id @default(cuid())
name String?
email String? @unique
image String?
accounts Account[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
model Account {
id String @id @default(cuid())
userId String
type String
provider String
providerAccountId String
user User? @relation(fields: [userId], references: [id], onDelete: Cascade)
@@unique([provider, providerAccountId])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment