mirror of
https://github.com/CompassConnections/Compass.git
synced 2026-03-02 13:58:18 -05:00
68 lines
1.6 KiB
TypeScript
68 lines
1.6 KiB
TypeScript
import type { NextAuthConfig } from "next-auth";
|
|
import Credentials from "next-auth/providers/credentials";
|
|
import { prisma } from "./lib/prisma";
|
|
import { compare } from "bcryptjs";
|
|
|
|
export default {
|
|
providers: [
|
|
Credentials({
|
|
name: "Credentials",
|
|
credentials: {
|
|
email: { label: "Email", type: "email" },
|
|
password: { label: "Password", type: "password" },
|
|
},
|
|
async authorize(credentials) {
|
|
if (!credentials?.email || !credentials?.password) {
|
|
throw new Error("Email and password are required");
|
|
}
|
|
|
|
const user = await prisma.user.findUnique({
|
|
where: { email: credentials.email as string },
|
|
});
|
|
|
|
if (!user || !user.password) {
|
|
throw new Error("Invalid email or password");
|
|
}
|
|
|
|
const isValid = await compare(
|
|
credentials.password as string,
|
|
user.password
|
|
);
|
|
|
|
if (!isValid) {
|
|
throw new Error("Invalid email or password");
|
|
}
|
|
|
|
return {
|
|
id: user.id,
|
|
email: user.email,
|
|
name: user.name,
|
|
image: user.image,
|
|
};
|
|
},
|
|
}),
|
|
],
|
|
callbacks: {
|
|
async session({ session, token }) {
|
|
if (token && session.user) {
|
|
session.user.id = token.sub!;
|
|
}
|
|
return session;
|
|
},
|
|
async jwt({ token, user }) {
|
|
if (user) {
|
|
token.sub = user.id;
|
|
}
|
|
return token;
|
|
},
|
|
},
|
|
session: {
|
|
strategy: "jwt",
|
|
},
|
|
pages: {
|
|
signIn: "/login",
|
|
error: "/login",
|
|
},
|
|
secret: process.env.NEXTAUTH_SECRET,
|
|
} satisfies NextAuthConfig;
|