el.xwx.moe/pages/api/auth/[...nextauth].ts

61 lines
1.3 KiB
TypeScript
Raw Normal View History

2023-02-06 11:59:23 -06:00
import { prisma } from "@/lib/db";
import NextAuth from "next-auth/next";
import CredentialsProvider from "next-auth/providers/credentials";
import { AuthOptions } from "next-auth";
import bcrypt from "bcrypt";
export const authOptions: AuthOptions = {
session: {
strategy: "jwt",
},
providers: [
CredentialsProvider({
type: "credentials",
credentials: {},
async authorize(credentials, req) {
const { email, password } = credentials as {
2023-02-08 15:11:33 -06:00
id: string;
2023-02-06 11:59:23 -06:00
email: string;
password: string;
};
console.log(email, password);
const findUser = await prisma.user.findFirst({
where: {
email: email,
},
});
console.log(findUser);
let passwordMatches: boolean = false;
if (findUser?.password) {
passwordMatches = bcrypt.compareSync(password, findUser.password);
}
if (passwordMatches) {
2023-02-08 15:11:33 -06:00
return {
id: findUser?.id,
name: findUser?.name,
email: findUser?.email,
};
2023-02-06 11:59:23 -06:00
} else return null as any;
},
}),
],
pages: {
2023-02-08 15:11:33 -06:00
signIn: "/login",
},
callbacks: {
session: async ({ session, token }) => {
session.user.id = token?.sub;
return session;
},
2023-02-06 11:59:23 -06:00
},
};
export default NextAuth(authOptions);