The Open Source DocuSign Alternative.
TypeScript digital-signaturedocument-signingdocusign-alternativee-signatureesignesignaturenext-authnextjsopen-sourcepades-standardpdfpdf-signpdf-signaturepostgresqlprismaself-hostedsigningtypescript
feat: send forgot password email
Ephraim Atta-Duncan9ef50f356e94
diff --git a/a.md b/a.mddeleted file mode 100644index 71580a3ef..000000000--- a/a.md+++ /dev/null@@ -1,65 +0,0 @@-// import { NextApiRequest, NextApiResponse } from 'next';--// import crypto from 'crypto';--// import { sendResetPassword } from '@documenso/lib/mail';-// import { defaultHandler, defaultResponder } from '@documenso/lib/server';-// import prisma from '@documenso/prisma';--// async function postHandler(req: NextApiRequest, res: NextApiResponse) {-// const { email } = req.body;-// const cleanEmail = email.toLowerCase();--// if (!cleanEmail || !/.+@.+/.test(cleanEmail)) {-// res.status(400).json({ message: 'Invalid email' });-// return;-// }--// const user = await prisma.user.findFirst({-// where: {-// email: cleanEmail,-// },-// });--// if (!user) {-// return res.status(200).json({ message: 'A password reset email has been sent.' });-// }--// const existingToken = await prisma.passwordResetToken.findFirst({-// where: {-// userId: user.id,-// createdAt: {-// gte: new Date(Date.now() - 1000 _ 60 _ 60),-// },-// },-// });--// if (existingToken) {-// return res.status(200).json({ message: 'A password reset email has been sent.' });-// }--// const token = crypto.randomBytes(64).toString('hex');-// const expiry = new Date();-// expiry.setHours(expiry.getHours() + 24); // Set expiry to one hour from now--// let passwordResetToken;-// try {-// passwordResetToken = await prisma.passwordResetToken.create({-// data: {-// token,-// expiry,-// userId: user.id,-// },-// });-// } catch (error) {-// return res.status(500).json({ message: 'Something went wrong' });-// }--// await sendResetPassword(user, passwordResetToken.token);--// return res.status(200).json({ message: 'A password reset email has been sent.' });-// }--// export default defaultHandler({-// POST: Promise.resolve({ default: defaultResponder(postHandler) }),-// });diff --git a/apps/web/src/components/forms/forgot-password.tsx b/apps/web/src/components/forms/forgot-password.tsxindex 852ff0301..61089b629 100644--- a/apps/web/src/components/forms/forgot-password.tsx+++ b/apps/web/src/components/forms/forgot-password.tsx@@ -51,8 +51,6 @@ export const ForgotPasswordForm = ({ className }: ForgotPasswordFormProps) => {email,});- reset();-toast({title: 'Password updated',description: 'Your password has been updated successfully.',@@ -63,6 +61,7 @@ export const ForgotPasswordForm = ({ className }: ForgotPasswordFormProps) => {setTimeout(resolve, 2000);});+ reset();router.push('/check-email');};diff --git a/packages/lib/server-only/auth/send-forgot-password.ts b/packages/lib/server-only/auth/send-forgot-password.tsnew file mode 100644index 000000000..88de43270--- /dev/null+++ b/packages/lib/server-only/auth/send-forgot-password.ts@@ -0,0 +1,53 @@+import { createElement } from 'react';++import { mailer } from '@documenso/email/mailer';+import { render } from '@documenso/email/render';+import { ForgotPasswordTemplate } from '@documenso/email/templates/forgot-password';+import { prisma } from '@documenso/prisma';++export interface SendForgotPasswordOptions {+ userId: number;+}++export const sendForgotPassword = async ({ userId }: SendForgotPasswordOptions) => {+ const user = await prisma.user.findFirstOrThrow({+ where: {+ id: userId,+ },+ include: {+ PasswordResetToken: {+ orderBy: {+ createdAt: 'desc',+ },+ take: 1,+ },+ },+ });++ if (!user) {+ throw new Error('User not found');+ }++ const token = user.PasswordResetToken[0].token;+ const assetBaseUrl = process.env.NEXT_PUBLIC_SITE_URL || 'http://localhost:3000';+ const resetPasswordLink = `${process.env.NEXT_PUBLIC_SITE_URL}/reset/${token}`;++ const template = createElement(ForgotPasswordTemplate, {+ assetBaseUrl,+ resetPasswordLink,+ });++ return await mailer.sendMail({+ to: {+ address: user.email,+ name: user.name || '',+ },+ from: {+ name: process.env.NEXT_PRIVATE_SMTP_FROM_NAME || 'Documenso',+ address: process.env.NEXT_PRIVATE_SMTP_FROM_ADDRESS || '[email protected]',+ },+ subject: 'Forgot Password?',+ html: render(template),+ text: render(template, { plainText: true }),+ });+};diff --git a/packages/lib/server-only/user/forgot-password.ts b/packages/lib/server-only/user/forgot-password.tsindex 8cbb13b82..e309c9006 100644--- a/packages/lib/server-only/user/forgot-password.ts+++ b/packages/lib/server-only/user/forgot-password.ts@@ -3,6 +3,8 @@ import crypto from 'crypto';import { prisma } from '@documenso/prisma';import { TForgotPasswordFormSchema } from '@documenso/trpc/server/profile-router/schema';+import { sendForgotPassword } from '../auth/send-forgot-password';+export const forgotPassword = async ({ email }: TForgotPasswordFormSchema) => {const user = await prisma.user.findFirstOrThrow({where: {@@ -31,10 +33,8 @@ export const forgotPassword = async ({ email }: TForgotPasswordFormSchema) => {const expiry = new Date();expiry.setHours(expiry.getHours() + 24); // Set expiry to one hour from now- let passwordResetToken;-try {- passwordResetToken = await prisma.passwordResetToken.create({+ await prisma.passwordResetToken.create({data: {token,expiry,@@ -45,8 +45,7 @@ export const forgotPassword = async ({ email }: TForgotPasswordFormSchema) => {throw new Error('Something went wrong');}- console.log('Password reset token: ', passwordResetToken);- // send an email to user with password token-- return passwordResetToken;+ return await sendForgotPassword({+ userId: user.id,+ });};