From 166cbc150fe8bf9c542b5243805f098dd0b3f02b Mon Sep 17 00:00:00 2001 From: Ephraim Atta-Duncan Date: Mon, 18 Sep 2023 14:31:04 +0000 Subject: [PATCH] feat: send email to user on successful password reset --- .../forgot-password/page.tsx | 1 + .../src/components/forms/forgot-password.tsx | 5 +-- .../template-reset-password.tsx | 4 +- packages/email/templates/reset-password.tsx | 15 +++---- .../server-only/auth/send-reset-password.ts | 44 +++++++++++++++++++ .../lib/server-only/user/reset-password.ts | 3 +- 6 files changed, 57 insertions(+), 15 deletions(-) create mode 100644 packages/lib/server-only/auth/send-reset-password.ts diff --git a/apps/web/src/app/(unauthenticated)/forgot-password/page.tsx b/apps/web/src/app/(unauthenticated)/forgot-password/page.tsx index 73cfd01ca..d72719066 100644 --- a/apps/web/src/app/(unauthenticated)/forgot-password/page.tsx +++ b/apps/web/src/app/(unauthenticated)/forgot-password/page.tsx @@ -5,6 +5,7 @@ import backgroundPattern from '~/assets/background-pattern.png'; import { ForgotPasswordForm } from '~/components/forms/forgot-password'; export default function ForgotPasswordPage() { + // TODO: Fix width reducing with screen size return (
diff --git a/apps/web/src/components/forms/forgot-password.tsx b/apps/web/src/components/forms/forgot-password.tsx index 61089b629..240f1ece1 100644 --- a/apps/web/src/components/forms/forgot-password.tsx +++ b/apps/web/src/components/forms/forgot-password.tsx @@ -43,10 +43,7 @@ export const ForgotPasswordForm = ({ className }: ForgotPasswordFormProps) => { const { mutateAsync: forgotPassword } = trpc.profile.forgotPassword.useMutation(); const onFormSubmit = async ({ email }: TForgotPasswordFormSchema) => { - // check if the email is available - // if not, throw an error - // if the email is available, create a password reset token and send an email - + // TODO: Handle error with try/catch await forgotPassword({ email, }); diff --git a/packages/email/template-components/template-reset-password.tsx b/packages/email/template-components/template-reset-password.tsx index 7dfc0ba78..ee2e8e7b1 100644 --- a/packages/email/template-components/template-reset-password.tsx +++ b/packages/email/template-components/template-reset-password.tsx @@ -3,8 +3,8 @@ import { Img, Section, Tailwind, Text } from '@react-email/components'; import * as config from '@documenso/tailwind-config'; export interface TemplateResetPasswordProps { - inviterName: string; - inviterEmail: string; + userName: string; + userEmail: string; assetBaseUrl: string; } diff --git a/packages/email/templates/reset-password.tsx b/packages/email/templates/reset-password.tsx index 6443661b9..0168b82e8 100644 --- a/packages/email/templates/reset-password.tsx +++ b/packages/email/templates/reset-password.tsx @@ -23,9 +23,8 @@ import { export type ResetPasswordTemplateProps = Partial; export const ResetPasswordTemplate = ({ - inviterName = 'Lucas Smith', - inviterEmail = 'lucas@documenso.com', - + userName = 'Lucas Smith', + userEmail = 'lucas@documenso.com', assetBaseUrl = 'http://localhost:3002', }: ResetPasswordTemplateProps) => { const previewText = `Password Reset Successful`; @@ -58,8 +57,8 @@ export const ResetPasswordTemplate = ({ /> @@ -68,9 +67,9 @@ export const ResetPasswordTemplate = ({
- Hi, {inviterName}{' '} - - ({inviterEmail}) + Hi, {userName}{' '} + + ({userEmail}) diff --git a/packages/lib/server-only/auth/send-reset-password.ts b/packages/lib/server-only/auth/send-reset-password.ts new file mode 100644 index 000000000..9cd09fe5a --- /dev/null +++ b/packages/lib/server-only/auth/send-reset-password.ts @@ -0,0 +1,44 @@ +import { createElement } from 'react'; + +import { mailer } from '@documenso/email/mailer'; +import { render } from '@documenso/email/render'; +import { ResetPasswordTemplate } from '@documenso/email/templates/reset-password'; +import { prisma } from '@documenso/prisma'; + +export interface SendResetPasswordOptions { + userId: number; +} + +export const sendResetPassword = async ({ userId }: SendResetPasswordOptions) => { + const user = await prisma.user.findFirstOrThrow({ + where: { + id: userId, + }, + }); + + if (!user) { + throw new Error('User not found'); + } + + const assetBaseUrl = process.env.NEXT_PUBLIC_SITE_URL || 'http://localhost:3000'; + + const template = createElement(ResetPasswordTemplate, { + assetBaseUrl, + userEmail: user.email, + userName: user.name || '', + }); + + 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 || 'noreply@documenso.com', + }, + subject: 'Password Reset Success!', + html: render(template), + text: render(template, { plainText: true }), + }); +}; diff --git a/packages/lib/server-only/user/reset-password.ts b/packages/lib/server-only/user/reset-password.ts index 7ce1bcb20..58a93749c 100644 --- a/packages/lib/server-only/user/reset-password.ts +++ b/packages/lib/server-only/user/reset-password.ts @@ -3,6 +3,7 @@ import { compare, hash } from 'bcrypt'; import { prisma } from '@documenso/prisma'; import { SALT_ROUNDS } from '../../constants/auth'; +import { sendResetPassword } from '../auth/send-reset-password'; export type ResetPasswordOptions = { token: string; @@ -61,6 +62,6 @@ export const resetPassword = async ({ token, password }: ResetPasswordOptions) = throw new Error('Unable to update password'); } - // await sendResetPasswordSuccessMail(foundToken.User); + await sendResetPassword({ userId: foundToken.userId }); return transactions; };