32 lines
1.1 KiB
TypeScript
32 lines
1.1 KiB
TypeScript
import NextAuth, { type NextAuthOptions } from "next-auth"
|
|
import Credentials from "next-auth/providers/credentials"
|
|
import { PrismaAdapter } from "@auth/prisma-adapter"
|
|
import prisma from "@/lib/prisma"
|
|
import { compare } from "bcryptjs"
|
|
|
|
export const authOptions: NextAuthOptions = {
|
|
// Adapter from @auth works with next-auth v4 as well
|
|
adapter: PrismaAdapter(prisma) as any,
|
|
session: { strategy: "jwt" },
|
|
providers: [
|
|
Credentials({
|
|
name: "Credentials",
|
|
credentials: {
|
|
email: { label: "Email", type: "text" },
|
|
password: { label: "Password", type: "password" },
|
|
},
|
|
async authorize(credentials) {
|
|
if (!credentials?.email || !credentials?.password) return null
|
|
const user = await prisma.user.findUnique({ where: { email: credentials.email } })
|
|
if (!user || !user.password) return null
|
|
const ok = await compare(credentials.password, user.password)
|
|
if (!ok) return null
|
|
return { id: user.id, email: user.email, name: user.name ?? undefined }
|
|
},
|
|
}),
|
|
],
|
|
}
|
|
|
|
const handler = NextAuth(authOptions)
|
|
export { handler as GET, handler as POST }
|