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

44 lines
1.1 KiB
TypeScript
Raw Normal View History

import previewEmail from "preview-email";
2021-07-31 14:33:18 +00:00
import { sendEmail } from "integrations/aws-ses";
import { render, plaintext } 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}`;
const [html, text] = await Promise.all([
render("forgot-password", { action_url: resetUrl, name: userName }),
plaintext("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,
text,
};
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,
text: msg.text,
});
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
}