cal.pub0.org/pages/api/auth/[...nextauth].tsx

72 lines
1.8 KiB
TypeScript

import NextAuth from "next-auth";
import Providers from "next-auth/providers";
import prisma from "../../../lib/prisma";
import { Session, verifyPassword } from "../../../lib/auth";
export default NextAuth({
session: {
jwt: true,
},
pages: {
signIn: "/auth/login",
signOut: "/auth/logout",
error: "/auth/error", // Error code passed in query string as ?error=
},
providers: [
Providers.Credentials({
name: "Calendso",
credentials: {
email: { label: "Email Address", type: "email", placeholder: "john.doe@example.com" },
password: { label: "Password", type: "password", placeholder: "Your super secure password" },
},
async authorize(credentials) {
const user = await prisma.user.findUnique({
where: {
email: credentials.email,
},
});
if (!user) {
throw new Error("No user found");
}
if (!user.password) {
throw new Error("Incorrect password");
}
const isValid = await verifyPassword(credentials.password, user.password);
if (!isValid) {
throw new Error("Incorrect password");
}
return {
id: user.id,
username: user.username,
email: user.email,
name: user.name,
};
},
}),
],
callbacks: {
async jwt(token, user) {
if (user) {
token.id = user.id;
token.username = user.username;
}
return token;
},
async session(session, token) {
const calendsoSession: Session = {
...session,
user: {
...session.user,
id: token.id as number,
username: token.username as string,
},
};
return calendsoSession;
},
},
});