mirror of
https://github.com/documenso/documenso.git
synced 2025-11-27 06:54:01 +10:00
Support runtime environment variables using server components. This will mean docker images can change env vars for runtime as required.
58 lines
1.5 KiB
TypeScript
58 lines
1.5 KiB
TypeScript
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';
|
|
|
|
import { getRuntimeEnv } from '../../universal/runtime-env/get-runtime-env';
|
|
|
|
export interface SendForgotPasswordOptions {
|
|
userId: number;
|
|
}
|
|
|
|
export const sendForgotPassword = async ({ userId }: SendForgotPasswordOptions) => {
|
|
const { NEXT_PUBLIC_WEBAPP_URL } = getRuntimeEnv();
|
|
|
|
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,
|
|
});
|
|
|
|
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: 'Forgot Password?',
|
|
html: render(template),
|
|
text: render(template, { plainText: true }),
|
|
});
|
|
};
|