Files
sign/apps/web/pages/api/auth/[...nextauth].ts

92 lines
2.3 KiB
TypeScript
Raw Normal View History

2023-01-10 18:52:04 +01:00
import { ErrorCode } from "@documenso/lib/auth";
import { verifyPassword } from "@documenso/lib/auth";
2023-04-04 22:02:32 +00:00
import prisma from "@documenso/prisma";
import NextAuth, { Session } from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";
import GitHubProvider from "next-auth/providers/github";
2022-12-08 22:42:19 +01:00
export default NextAuth({
2023-01-11 14:36:59 +01:00
secret: process.env.AUTH_SECRET,
pages: {
signIn: "/login",
signOut: "/login",
error: "/auth/error", // Error code passed in query string as ?error=
verifyRequest: "/auth/verify-request", // (used for check email message)
},
2022-12-08 22:42:19 +01:00
providers: [
2023-01-10 18:52:04 +01:00
CredentialsProvider({
2023-01-11 14:36:59 +01:00
id: "credentials",
2023-01-10 18:52:04 +01:00
name: "Documenso.com Login",
type: "credentials",
credentials: {
email: {
label: "Email Address",
type: "email",
placeholder: "john.doe@example.com",
},
password: {
label: "Password",
type: "password",
2023-04-04 22:02:32 +00:00
placeholder: "Select a password. Here is some inspiration: https://xkcd.com/936/",
2023-01-10 18:52:04 +01:00
},
},
async authorize(credentials: any) {
if (!credentials) {
console.error("Credential missing in authorize()");
throw new Error(ErrorCode.InternalServerError);
}
const user = await prisma.user.findUnique({
where: {
email: credentials.email.toLowerCase(),
},
select: {
id: true,
email: true,
password: true,
name: true,
},
});
if (!user) {
throw new Error(ErrorCode.UserNotFound);
}
if (!user.password) {
throw new Error(ErrorCode.UserMissingPassword);
}
2023-04-04 22:02:32 +00:00
const isCorrectPassword = await verifyPassword(credentials.password, user.password);
2023-01-10 18:52:04 +01:00
if (!isCorrectPassword) {
throw new Error(ErrorCode.IncorrectPassword);
}
return {
2023-01-19 14:13:21 +01:00
id: user.id,
2023-01-10 18:52:04 +01:00
email: user.email,
name: user.name,
};
},
2022-12-08 22:42:19 +01:00
}),
],
2023-01-19 14:13:21 +01:00
callbacks: {
async jwt({ token, user, account }) {
return {
...token,
};
},
async session({ session, token }) {
const documensoSession: Session = {
...session,
user: {
...session.user,
},
};
documensoSession.expires;
return documensoSession;
},
},
2022-12-08 22:42:19 +01:00
});