mirror of
https://github.com/documenso/documenso.git
synced 2025-11-10 04:22:32 +10:00
66 lines
1.7 KiB
TypeScript
66 lines
1.7 KiB
TypeScript
import { createElement } from 'react';
|
|
|
|
import { msg } from '@lingui/macro';
|
|
|
|
import { mailer } from '@documenso/email/mailer';
|
|
import { ForgotPasswordTemplate } from '@documenso/email/templates/forgot-password';
|
|
import { prisma } from '@documenso/prisma';
|
|
|
|
import { getI18nInstance } from '../../client-only/providers/i18n.server';
|
|
import { NEXT_PUBLIC_WEBAPP_URL } from '../../constants/app';
|
|
import { renderEmailWithI18N } from '../../utils/render-email-with-i18n';
|
|
|
|
export interface SendForgotPasswordOptions {
|
|
userId: string;
|
|
}
|
|
|
|
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 = NEXT_PUBLIC_WEBAPP_URL() || 'http://localhost:3000';
|
|
const resetPasswordLink = `${NEXT_PUBLIC_WEBAPP_URL()}/reset-password/${token}`;
|
|
|
|
const template = createElement(ForgotPasswordTemplate, {
|
|
assetBaseUrl,
|
|
resetPasswordLink,
|
|
});
|
|
|
|
const [html, text] = await Promise.all([
|
|
renderEmailWithI18N(template),
|
|
renderEmailWithI18N(template, { plainText: true }),
|
|
]);
|
|
|
|
const i18n = await getI18nInstance();
|
|
|
|
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: i18n._(msg`Forgot Password?`),
|
|
html,
|
|
text,
|
|
});
|
|
};
|