import NextAuth from 'next-auth';
import CredentialsProvider from 'next-auth/providers/credentials';
import bcrypt from 'bcryptjs';
import prisma from "@/lib/prisma"
export const { handlers: {GET, POST }, signIn, signOut, auth } = NextAuth({
providers: [
CredentialsProvider({
name: 'Credentials',
credentials: {
email: { label: "Email", type: "text" },
password: { label: "Password", type: "password" }
},
async authorize(credentials) {
// Find user by email
const user = await prisma.user.findUnique({
where: { email: credentials.email }
});
// Check if user exists and password matches
if (user && bcrypt.compareSync(credentials.password, user.password)) {
return { id: user.id, email: user.email };
}
// If no user or password mismatch, return null
return null;
}
})
],
pages: {
signIn: '/login', // Custom login page
// error: '/error', // Error page
},
session: {
strategy: 'jwt',
},
callbacks: {
async jwt({ token, user }) {
if (user) {
token.id = user.id;
}
return token;
},
async session({ session, token }) {
if (token) {
session.id = token.id;
}
return session;
}
}
})