shellphone.app/mailers/forgot-password-mailer.ts

39 lines
974 B
TypeScript
Raw Normal View History

import previewEmail from "preview-email";
2021-07-31 14:33:18 +00:00
import { sendEmail } from "integrations/aws-ses";
2021-10-30 11:37:16 +00:00
import { render } from "./renderer";
2021-07-31 14:33:18 +00:00
type ResetPasswordMailer = {
to: string;
token: string;
userName: string;
};
2021-07-31 14:33:18 +00:00
export async function forgotPasswordMailer({ to, token, userName }: ResetPasswordMailer) {
2021-07-31 14:33:18 +00:00
// In production, set APP_ORIGIN to your production server origin
const origin = process.env.APP_ORIGIN || process.env.BLITZ_DEV_SERVER_ORIGIN;
const resetUrl = `${origin}/reset-password?token=${token}`;
2021-10-30 11:37:16 +00:00
const html = await render("forgot-password", { action_url: resetUrl, name: userName });
2021-07-31 14:33:18 +00:00
const msg = {
from: "mokhtar@shellphone.app",
2021-07-31 14:33:18 +00:00
to,
subject: "Reset your password",
html,
};
2021-07-31 14:33:18 +00:00
return {
async send() {
if (process.env.NODE_ENV === "production") {
await sendEmail({
recipients: [msg.to],
subject: msg.subject,
html: msg.html,
});
2021-07-31 14:33:18 +00:00
} else {
// Preview email in the browser
await previewEmail(msg);
2021-07-31 14:33:18 +00:00
}
},
};
2021-07-31 14:33:18 +00:00
}