55 lines
1.5 KiB
TypeScript
55 lines
1.5 KiB
TypeScript
import { PrismaAdapter } from "@next-auth/prisma-adapter";
|
|
import CredentialsProvider from "next-auth/providers/credentials";
|
|
import type { NextAuthOptions } from "next-auth";
|
|
import bcrypt from "bcryptjs";
|
|
import { prisma } from "./prisma";
|
|
|
|
export const authOptions: NextAuthOptions = {
|
|
adapter: PrismaAdapter(prisma),
|
|
session: { strategy: "jwt" },
|
|
pages: {
|
|
signIn: "/login",
|
|
},
|
|
callbacks: {
|
|
async jwt({ token, user }) {
|
|
if (user) {
|
|
token.id = user.id;
|
|
}
|
|
if (!token.id) {
|
|
token.id = token.sub ?? "";
|
|
}
|
|
return token;
|
|
},
|
|
async session({ session, token }) {
|
|
if (session.user) {
|
|
session.user.id = token.id || "";
|
|
}
|
|
return session;
|
|
},
|
|
},
|
|
providers: [
|
|
CredentialsProvider({
|
|
name: "Credentials",
|
|
credentials: {
|
|
email: { label: "Email", type: "email" },
|
|
password: { label: "Password", type: "password" },
|
|
},
|
|
async authorize(credentials) {
|
|
const email = credentials?.email?.toLowerCase().trim();
|
|
const password = credentials?.password;
|
|
if (!email || !password) return null;
|
|
|
|
const user = await prisma.user.findUnique({ where: { email } });
|
|
if (!user) return null;
|
|
|
|
if (!user.passwordHash) return null;
|
|
|
|
const valid = await bcrypt.compare(password, user.passwordHash);
|
|
if (!valid) return null;
|
|
|
|
return { id: user.id, name: user.name, email: user.email };
|
|
},
|
|
}),
|
|
],
|
|
};
|