46 lines
1.2 KiB
TypeScript
46 lines
1.2 KiB
TypeScript
import NextAuth from "next-auth";
|
|
import { PrismaAdapter } from "@auth/prisma-adapter";
|
|
import Credentials from "next-auth/providers/credentials";
|
|
import { db } from "@/lib/db";
|
|
import bcrypt from "bcryptjs";
|
|
import { authConfig } from "@/auth.config";
|
|
|
|
export const { handlers, auth, signIn, signOut } = NextAuth({
|
|
...authConfig,
|
|
adapter: PrismaAdapter(db) as any,
|
|
providers: [
|
|
Credentials({
|
|
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 db.user.findUnique({
|
|
where: { email: credentials.email as string },
|
|
});
|
|
|
|
if (!user || !user.passwordHash || !user.isActive) return null;
|
|
|
|
const isValid = await bcrypt.compare(
|
|
credentials.password as string,
|
|
user.passwordHash
|
|
);
|
|
|
|
if (!isValid) return null;
|
|
|
|
return {
|
|
id: user.id,
|
|
email: user.email,
|
|
name: user.name,
|
|
image: user.image,
|
|
role: user.role,
|
|
mustChangePassword: user.mustChangePassword,
|
|
};
|
|
},
|
|
}),
|
|
],
|
|
});
|