import NextAuth from 'next-auth'; import { authConfig } from './auth.config'; import CredentialsProvider from 'next-auth/providers/credentials'; import bcrypt from 'bcryptjs'; import { prisma } from './lib/prisma'; export const { handlers, auth, signIn, signOut } = NextAuth({ ...authConfig, providers: [ CredentialsProvider({ name: 'Credentials', credentials: { email: { label: "Email", type: "email" }, 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 as string } }); if (!user || !user.password) return null; const passwordsMatch = await bcrypt.compare( credentials.password as string, user.password ); if (passwordsMatch) return user; return null; }, }), ], callbacks: { async jwt({ token, user }) { if (user) { token.role = (user as any).role; token.id = user.id; } return token; }, async session({ session, token }) { if (token) { (session.user as any).role = token.role; (session.user as any).id = token.id; } return session; }, }, });