f75178de69
CI / build (push) Has been cancelled
Invite and welcome emails signed a 10-minute sign-in token, so the link was usually dead by the time someone opened the mail and could never reach the set-password page. Invitations now use a 7-day single-use token, while requested sign-in links stay at 10 minutes. Expired or already-used links also explain themselves on the sign-in page and point at requesting a fresh one. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
97 lines
3.3 KiB
TypeScript
97 lines
3.3 KiB
TypeScript
/**
|
|
* Welcome mail for a newly added team member.
|
|
*
|
|
* An admin adding a person should not have to copy a link out of the admin
|
|
* screen: the account, its username and a one-time sign-in link go straight to
|
|
* the person's inbox. Delivery failures are reported back to the admin (who can
|
|
* still copy the link), never swallowed.
|
|
*/
|
|
|
|
import { getEmailTransportSettings } from "@/db/settings";
|
|
import { resolveEmailTransport } from "@/lib/email";
|
|
|
|
export interface WelcomeUser {
|
|
id: string;
|
|
email: string;
|
|
username: string;
|
|
firstName: string;
|
|
displayName?: string | null;
|
|
}
|
|
|
|
export interface WelcomeResult {
|
|
delivered: boolean;
|
|
transport: string | null;
|
|
}
|
|
|
|
function escapeHtml(value: string): string {
|
|
return value
|
|
.replaceAll("&", "&")
|
|
.replaceAll("<", "<")
|
|
.replaceAll(">", ">")
|
|
.replaceAll('"', """)
|
|
.replaceAll("'", "'");
|
|
}
|
|
|
|
/** Send the sign-in details. Never throws; the caller reports the outcome. */
|
|
export async function sendWelcomeEmail(
|
|
user: WelcomeUser,
|
|
url: string,
|
|
): Promise<WelcomeResult> {
|
|
try {
|
|
const transport = resolveEmailTransport(await getEmailTransportSettings());
|
|
const name = escapeHtml(user.firstName || user.displayName || user.email);
|
|
const login = escapeHtml(user.username || user.email);
|
|
const email = escapeHtml(user.email);
|
|
const link = escapeHtml(url);
|
|
|
|
await transport.send({
|
|
to: user.email,
|
|
subject: "Your Media Team Hub sign-in details",
|
|
html: `<!doctype html>
|
|
<html>
|
|
<body style="font-family: system-ui, sans-serif; line-height: 1.5; color: #111827;">
|
|
<h1 style="font-size: 20px;">Welcome, ${name}</h1>
|
|
<p>An account has been created for you on Media Team Hub, the team's scheduling and training site.</p>
|
|
<p>
|
|
<strong>Sign in:</strong> ${email}<br />
|
|
<strong>Username:</strong> ${login}
|
|
</p>
|
|
<p>
|
|
<a href="${link}" style="display:inline-block;padding:10px 16px;background:#932822;color:#fff;border-radius:6px;text-decoration:none;">Sign in</a>
|
|
</p>
|
|
<p style="font-size: 12px; color: #6b7280;">
|
|
The link expires in 7 days and works once. If it has expired, open the
|
|
sign-in page and request a fresh link with this email address. After
|
|
signing in you can set a password from your profile.
|
|
</p>
|
|
<p style="font-size: 12px; color: #6b7280;">Media Team Hub · Glenloch</p>
|
|
</body>
|
|
</html>`,
|
|
text: [
|
|
`Welcome, ${user.firstName || user.displayName || user.email}`,
|
|
"",
|
|
"An account has been created for you on Media Team Hub, the team's scheduling and training site.",
|
|
"",
|
|
`Sign in: ${user.email}`,
|
|
`Username: ${user.username || user.email}`,
|
|
"",
|
|
`Open this link to sign in (expires in 7 days, single use):`,
|
|
url,
|
|
"",
|
|
"If it has expired, open the sign-in page and request a fresh link with this email address. After signing in you can set a password from your profile.",
|
|
"",
|
|
"Media Team Hub · Glenloch",
|
|
].join("\n"),
|
|
templateKey: "user.welcome",
|
|
dedupeKey: `user.welcome:${user.id}:${Date.now()}`,
|
|
});
|
|
return { delivered: true, transport: transport.name };
|
|
} catch (error) {
|
|
console.error(
|
|
`[welcome] delivery failed for ${user.email}:`,
|
|
error instanceof Error ? error.message : error,
|
|
);
|
|
return { delivered: false, transport: null };
|
|
}
|
|
}
|