40 lines
1.0 KiB
TypeScript
40 lines
1.0 KiB
TypeScript
import NextAuth from "next-auth";
|
|
import Credentials from "next-auth/providers/credentials";
|
|
import { compare } from "bcryptjs";
|
|
import { prisma } from "@/lib/db";
|
|
import authConfig from "@/lib/auth.config";
|
|
import { loginSchema } from "@/lib/validations/auth";
|
|
|
|
export const { handlers, auth, signIn, signOut } = NextAuth({
|
|
...authConfig,
|
|
providers: [
|
|
Credentials({
|
|
credentials: {
|
|
email: { label: "Email", type: "email" },
|
|
password: { label: "Password", type: "password" },
|
|
},
|
|
async authorize(credentials) {
|
|
const parsed = loginSchema.safeParse(credentials);
|
|
|
|
if (!parsed.success) {
|
|
return null;
|
|
}
|
|
|
|
const user = await prisma.user.findUnique({
|
|
where: { email: parsed.data.email },
|
|
});
|
|
|
|
if (!user || !(await compare(parsed.data.password, user.password))) {
|
|
return null;
|
|
}
|
|
|
|
return {
|
|
id: user.id,
|
|
email: user.email,
|
|
name: user.email,
|
|
};
|
|
},
|
|
}),
|
|
],
|
|
});
|