Files
sign/packages/lib/server-only/auth/send-forgot-password.ts

67 lines
1.8 KiB
TypeScript
Raw Normal View History

2023-09-18 12:14:55 +00:00
import { createElement } from 'react';
2025-01-02 15:33:37 +11:00
import { msg } from '@lingui/core/macro';
2023-09-18 12:14:55 +00:00
import { mailer } from '@documenso/email/mailer';
import { ForgotPasswordTemplate } from '@documenso/email/templates/forgot-password';
import { prisma } from '@documenso/prisma';
2025-01-02 15:33:37 +11:00
import { getI18nInstance } from '../../client-only/providers/i18n-server';
import { NEXT_PUBLIC_WEBAPP_URL } from '../../constants/app';
2025-01-02 15:33:37 +11:00
import { env } from '../../utils/env';
import { renderEmailWithI18N } from '../../utils/render-email-with-i18n';
2023-09-18 12:14:55 +00:00
export interface SendForgotPasswordOptions {
userId: number;
}
export const sendForgotPassword = async ({ userId }: SendForgotPasswordOptions) => {
const user = await prisma.user.findFirstOrThrow({
where: {
id: userId,
},
include: {
2025-01-13 13:41:53 +11:00
passwordResetTokens: {
2023-09-18 12:14:55 +00:00
orderBy: {
createdAt: 'desc',
},
take: 1,
},
},
});
if (!user) {
throw new Error('User not found');
}
2025-01-13 13:41:53 +11:00
const token = user.passwordResetTokens[0].token;
const assetBaseUrl = NEXT_PUBLIC_WEBAPP_URL() || 'http://localhost:3000';
const resetPasswordLink = `${NEXT_PUBLIC_WEBAPP_URL()}/reset-password/${token}`;
2023-09-18 12:14:55 +00:00
const template = createElement(ForgotPasswordTemplate, {
assetBaseUrl,
resetPasswordLink,
});
const [html, text] = await Promise.all([
renderEmailWithI18N(template),
renderEmailWithI18N(template, { plainText: true }),
]);
const i18n = await getI18nInstance();
2023-09-18 12:14:55 +00:00
return await mailer.sendMail({
to: {
address: user.email,
name: user.name || '',
},
from: {
2025-01-02 15:33:37 +11:00
name: env('NEXT_PRIVATE_SMTP_FROM_NAME') || 'Documenso',
address: env('NEXT_PRIVATE_SMTP_FROM_ADDRESS') || 'noreply@documenso.com',
2023-09-18 12:14:55 +00:00
},
subject: i18n._(msg`Forgot Password?`),
html,
text,
2023-09-18 12:14:55 +00:00
});
};