As Cross Events' own sign-in sends: when, the site, rough location and device, with what to do if it wasn't them. WhatsApp too for a verified number. Admins can turn it off (signin_notices). Fire-and-forget: never holds up or fails a sign-in. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
226 lines
16 KiB
TypeScript
226 lines
16 KiB
TypeScript
/**
|
|
* The emails Accounts sends (1.4.0), styled as Cross Events' (apps/events/backend/src/utils/email.js):
|
|
* a header in the church's colour with its logo or name, a white card, a button for the one thing
|
|
* to do with the link written out under it, and a footer with the church's contact details.
|
|
*
|
|
* Every email also has the plain text it always had (some mail apps show only that, and the tests
|
|
* read codes and links from it). Everything put into the HTML is escaped; colours and links are
|
|
* checked first.
|
|
*/
|
|
import type { Ctx } from '../context.js';
|
|
import { church } from '../church/profile.js';
|
|
|
|
export type Built = { subject: string; text: string; html: string };
|
|
|
|
type Brand = { name: string; colour: string; accent: string; logoUrl: string | null; email: string | null; phone: string | null; url: string };
|
|
|
|
const FONT = `-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Helvetica,Arial,sans-serif`;
|
|
const MONO = `'SFMono-Regular',Consolas,'Liberation Mono',Menlo,monospace`;
|
|
const HEX = /^#[0-9a-fA-F]{6}$/;
|
|
|
|
export const esc = (s: unknown) => String(s ?? '')
|
|
.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''');
|
|
|
|
/** Only http(s) links go in an href. */
|
|
const safeUrl = (u: string) => (/^https?:\/\//i.test(u) ? u : '#');
|
|
|
|
/** A darker shade of a #rrggbb colour, for the header's gradient. */
|
|
function darker(hex: string, by = 0.25): string {
|
|
const n = parseInt(hex.slice(1), 16);
|
|
const ch = (shift: number) => Math.max(0, Math.round(((n >> shift) & 255) * (1 - by)));
|
|
return `#${[ch(16), ch(8), ch(0)].map((c) => c.toString(16).padStart(2, '0')).join('')}`;
|
|
}
|
|
|
|
async function brand(ctx: Ctx): Promise<Brand> {
|
|
const base = ctx.config.publicUrl.replace(/\/+$/, '');
|
|
try {
|
|
const c = await church(ctx);
|
|
const logo = c.branding.logoUrl;
|
|
return {
|
|
name: c.name || 'Your church',
|
|
colour: HEX.test(c.branding.colours.primary) ? c.branding.colours.primary : '#1f4e79',
|
|
accent: c.branding.colours.accent && HEX.test(c.branding.colours.accent) ? c.branding.colours.accent : '#f2a900',
|
|
logoUrl: logo ? (logo.startsWith('/') ? `${base}${logo}` : /^https:\/\//i.test(logo) ? logo : null) : null,
|
|
email: c.contact.email ?? null,
|
|
phone: c.contact.phone ?? null,
|
|
url: base,
|
|
};
|
|
} catch {
|
|
return { name: 'Your church', colour: '#1f4e79', accent: '#f2a900', logoUrl: null, email: null, phone: null, url: base };
|
|
}
|
|
}
|
|
|
|
/** The page around an email's body (Cross Events' layout). */
|
|
function layout(b: Brand, body: string, preheader: string): string {
|
|
const header = b.logoUrl
|
|
? `<img src="${esc(b.logoUrl)}" alt="${esc(b.name)}" height="56" style="display:block;margin:0 auto;max-height:56px;max-width:240px;border:0">`
|
|
: `<h1 style="color:#ffffff;font-size:26px;font-weight:800;margin:0;letter-spacing:-0.5px;font-family:${FONT};text-shadow:0 1px 3px rgba(0,0,0,0.3)">${esc(b.name)}</h1>`;
|
|
const contact = [
|
|
esc(b.name),
|
|
b.email ? `<a href="mailto:${esc(b.email)}" style="color:#94a3b8;text-decoration:underline">${esc(b.email)}</a>` : '',
|
|
b.phone ? esc(b.phone) : '',
|
|
].filter(Boolean).join(' • ');
|
|
return `<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8"/>
|
|
<meta name="viewport" content="width=device-width,initial-scale=1.0"/>
|
|
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
|
|
<title>${esc(b.name)}</title>
|
|
</head>
|
|
<body style="margin:0;padding:0;background-color:#f1f5f9;-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%">
|
|
<div style="display:none;font-size:1px;line-height:1px;max-height:0;max-width:0;opacity:0;overflow:hidden;mso-hide:all">${esc(preheader)}‌ ‌ ‌ </div>
|
|
<table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="background-color:#f1f5f9">
|
|
<tr><td align="center" style="padding:40px 16px">
|
|
<table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="max-width:600px">
|
|
<tr><td style="background:${b.colour};background:linear-gradient(135deg,${b.colour} 0%,${darker(b.colour)} 100%);border-radius:12px 12px 0 0;padding:36px 48px;text-align:center">
|
|
${header}
|
|
<p style="color:#ffffff;font-size:13px;font-weight:500;margin:10px 0 0 0;font-family:${FONT};letter-spacing:0.3px;opacity:0.9">Your ${esc(b.name)} account</p>
|
|
</td></tr>
|
|
<tr><td style="background:#ffffff;border-left:1px solid #e2e8f0;border-right:1px solid #e2e8f0;padding:44px 48px 36px 48px">
|
|
<div style="font-family:${FONT};color:#1e293b;font-size:15px;line-height:1.75">
|
|
${body}
|
|
</div>
|
|
</td></tr>
|
|
<tr><td style="background:#f8fafc;border:1px solid #e2e8f0;border-top:none;border-radius:0 0 12px 12px;padding:24px 48px;text-align:center">
|
|
<p style="color:#94a3b8;font-size:12px;margin:0 0 4px 0;font-family:${FONT}">${contact}</p>
|
|
<p style="color:#cbd5e1;font-size:11px;margin:6px 0 0 0;font-family:${FONT}">You're getting this because you have an account with ${esc(b.name)}, used to sign in to its sites.</p>
|
|
</td></tr>
|
|
</table>
|
|
</td></tr>
|
|
</table>
|
|
</body>
|
|
</html>`;
|
|
}
|
|
|
|
const title = (t: string, sub?: string) =>
|
|
`<p style="font-size:22px;font-weight:800;color:#0f172a;margin:0 0 8px 0;letter-spacing:-0.3px">${esc(t)}</p>` +
|
|
(sub ? `<p style="color:#64748b;font-size:14px;margin:0 0 28px 0">${esc(sub)}</p>` : '');
|
|
const hello = (name?: string | null) => `<p style="margin:0 0 8px 0;color:#374151">Hello <strong>${esc(name || 'there')}</strong>,</p>`;
|
|
const para = (html: string) => `<p style="margin:0 0 20px 0;color:#374151">${html}</p>`;
|
|
const divider = () => `<div style="border-top:1px solid #f1f5f9;margin:28px 0"></div>`;
|
|
const small = (html: string) => `<p style="margin:0;font-size:13px;color:#94a3b8">${html}</p>`;
|
|
|
|
function button(b: Brand, label: string, url: string): string {
|
|
const href = esc(safeUrl(url));
|
|
return `<table role="presentation" cellpadding="0" cellspacing="0" border="0" style="margin:28px auto 8px auto">
|
|
<tr><td align="center" style="border-radius:8px;background-color:${b.colour}">
|
|
<a href="${href}" target="_blank" style="display:inline-block;padding:14px 36px;font-size:15px;font-weight:700;color:#ffffff;text-decoration:none;border-radius:8px;font-family:${FONT};letter-spacing:0.2px">${esc(label)}</a>
|
|
</td></tr>
|
|
</table>
|
|
<p style="text-align:center;margin:4px 0 0 0;font-size:12px;color:#94a3b8;word-break:break-all">Or copy this link: <a href="${href}" style="color:${b.colour}">${esc(url)}</a></p>`;
|
|
}
|
|
|
|
/** Info, a warning ("if this wasn't you") or neutral: these colours never follow the church's. */
|
|
function callout(html: string, kind: 'info' | 'warning' | 'success' = 'info'): string {
|
|
const s = { info: ['#eff6ff', '#3b82f6', '#1e40af'], warning: ['#fffbeb', '#f59e0b', '#92400e'], success: ['#f0fdf4', '#22c55e', '#166534'] }[kind];
|
|
return `<div style="background:${s[0]};border-left:4px solid ${s[1]};border-radius:0 6px 6px 0;padding:14px 18px;margin:24px 0;color:${s[2]};font-size:14px;line-height:1.6">${html}</div>`;
|
|
}
|
|
|
|
const code6 = (code: string) =>
|
|
`<p style="margin:8px 0 24px 0;font-size:34px;font-weight:800;letter-spacing:8px;color:#0f172a;text-align:center;font-family:${MONO}">${esc(code)}</p>`;
|
|
|
|
const contactLine = (b: Brand) => (b.email ? ` at <a href="mailto:${esc(b.email)}" style="color:${b.colour}">${esc(b.email)}</a>` : '');
|
|
|
|
// ─── The emails ───────────────────────────────────────────────────────────────
|
|
|
|
/** A sign-in or confirm-your-email code (the text is the WhatsApp message too). */
|
|
export async function codeEmail(ctx: Ctx, o: { name?: string | null; code: string; text: string; purpose: 'sign_in' | 'verify_email' | 'setup'; minutes: number }): Promise<Built> {
|
|
const b = await brand(ctx);
|
|
const what = o.purpose === 'verify_email' ? 'confirm your email address' : 'sign in';
|
|
const body = title('Your code', o.purpose === 'verify_email' ? 'To confirm your email address' : `To sign in to ${b.name}`)
|
|
+ hello(o.name)
|
|
+ para(`Enter this code to ${esc(what)}. It works for <strong>${o.minutes} minutes</strong>.`)
|
|
+ code6(o.code)
|
|
+ divider()
|
|
+ small(`Never share this code with anyone, not even someone from the church. If you didn't ask for it, you can ignore this email: nobody can sign in without your password${o.purpose === 'sign_in' ? ' and this code' : ''}.`);
|
|
return { subject: `Your code: ${o.code}`, text: o.text, html: layout(b, body, `Your code is ${o.code}. It works for ${o.minutes} minutes.`) };
|
|
}
|
|
|
|
export async function resetPasswordEmail(ctx: Ctx, o: { name?: string | null; url: string; minutes: number }): Promise<Built> {
|
|
const b = await brand(ctx);
|
|
const body = title('Reset your password', 'Someone (we hope you) asked to reset it.')
|
|
+ hello(o.name)
|
|
+ para(`Choose a new password with the button below. The link works for <strong>${o.minutes} minutes</strong>, once.`)
|
|
+ button(b, 'Choose a new password', o.url)
|
|
+ divider()
|
|
+ small(`If you didn't ask, you can ignore this email: your password stays the same. Worried? Let the church office know${contactLine(b)}.`);
|
|
const text = `Someone (we hope you) asked to reset the password for your account.\n\nChoose a new password here (the link works for ${o.minutes} minutes):\n${o.url}\n\nIf you didn't ask, you can ignore this email: your password stays the same.`;
|
|
return { subject: 'Reset your password', text, html: layout(b, body, `Choose a new password. The link works for ${o.minutes} minutes.`) };
|
|
}
|
|
|
|
/** A product (Cross Events) made someone an account: choose a password. */
|
|
export async function newAccountEmail(ctx: Ctx, o: { name: string; url: string; days: number }): Promise<Built> {
|
|
const b = await brand(ctx);
|
|
const body = title(`Welcome to ${b.name}`, 'An account has been made for you.')
|
|
+ hello(o.name)
|
|
+ para(`${esc(b.name)} has made you an account. It signs you in to all of the church's sites, such as events and registrations. Choose your password to start using it.`)
|
|
+ button(b, 'Choose my password', o.url)
|
|
+ callout(`The link works for <strong>${o.days} days</strong>. After that, use <em>Forgot your password?</em> on the sign-in page.`, 'info')
|
|
+ small('If you weren\'t expecting this, you can ignore this email.');
|
|
const text = `Hello ${o.name},\n\n${b.name} has made you an account. Choose your password here (the link works for ${o.days} days):\n${o.url}\n\nIf you weren't expecting this, you can ignore this email.`;
|
|
return { subject: `Your account at ${b.name}`, text, html: layout(b, body, `Choose your password for your ${b.name} account.`) };
|
|
}
|
|
|
|
export async function confirmNewEmail(ctx: Ctx, o: { name?: string | null; url: string }): Promise<Built> {
|
|
const b = await brand(ctx);
|
|
const body = title('Confirm your new email address')
|
|
+ hello(o.name)
|
|
+ para('You asked to sign in with this email address from now on. Confirm it\'s yours with the button below; until then you keep signing in with your old one.')
|
|
+ button(b, 'Confirm this address', o.url)
|
|
+ divider()
|
|
+ small('The link works for 24 hours. If you didn\'t ask for this, you can ignore this email.');
|
|
const text = `Confirm this is your new email address for signing in:\n${o.url}\n\nThe link works for 24 hours.`;
|
|
return { subject: 'Confirm your new email address', text, html: layout(b, body, 'Confirm your new sign-in email address.') };
|
|
}
|
|
|
|
export async function emailChangedNotice(ctx: Ctx, o: { name?: string | null; newEmail: string }): Promise<Built> {
|
|
const b = await brand(ctx);
|
|
const body = title('Your sign-in email address changed')
|
|
+ hello(o.name)
|
|
+ para(`Your account now signs in with <strong>${esc(o.newEmail)}</strong>. Emails about your account go there from now on.`)
|
|
+ callout(`<strong>Wasn't you?</strong> Contact the church office straight away${contactLine(b)}.`, 'warning');
|
|
const text = `Your account now signs in with ${o.newEmail}. If you didn't do this, contact the church office straight away.`;
|
|
return { subject: 'Your sign-in email address changed', text, html: layout(b, body, `Your account now signs in with ${o.newEmail}.`) };
|
|
}
|
|
|
|
export async function exportReadyEmail(ctx: Ctx, o: { name?: string | null; url: string; days: number }): Promise<Built> {
|
|
const b = await brand(ctx);
|
|
const body = title('Your data is ready', 'The copy you asked for')
|
|
+ hello(o.name)
|
|
+ para('The copy of your data you asked for is ready. Download it from your account page.')
|
|
+ button(b, 'Download my data', o.url)
|
|
+ callout(`It's kept for <strong>${o.days} days</strong>, then deleted. You need to be signed in to download it.`, 'info');
|
|
const text = `The copy of your data you asked for is ready. Download it from your account page (it's kept for ${o.days} days):\n${o.url}`;
|
|
return { subject: 'Your data is ready to download', text, html: layout(b, body, 'The copy of your data you asked for is ready.') };
|
|
}
|
|
|
|
/** A test from the admin page or from a linked product (`from`: "Cross Events"). */
|
|
export async function testEmail(ctx: Ctx, o: { from?: string } = {}): Promise<Built> {
|
|
const b = await brand(ctx);
|
|
const body = title('Email works', o.from ? `A test sent from ${o.from}` : 'A test from Accounts')
|
|
+ para(`This is a test from ${esc(b.name)}'s sign-in (Accounts)${o.from ? `, sent from ${esc(o.from)}` : ''}.`)
|
|
+ callout('Email is set up: sign-in codes, password links and security notices can be sent.', 'success');
|
|
const text = o.from
|
|
? `This is a test from ${b.name}'s sign-in (Accounts), sent from ${o.from}. Email works: sign-in codes and password links can be sent.`
|
|
: 'Email from Accounts works.';
|
|
return { subject: o.from ? `Test email from ${b.name}'s sign-in` : 'Test email from Accounts', text, html: layout(b, body, 'A test email: email works.') };
|
|
}
|
|
|
|
/** A sign-in happened (1.6.0, as Cross Events' "New login detected"). `whatsapp`: the same in a few lines. */
|
|
export async function newSignInEmail(ctx: Ctx, o: { name?: string | null; when: string; location: string; device: string; site: string | null }): Promise<Built & { whatsapp: string }> {
|
|
const b = await brand(ctx);
|
|
const row = (k: string, v: string) => `<tr><td style="padding:10px 14px;border-top:1px solid #f1f5f9;color:#64748b;font-size:13px;font-weight:600;width:34%">${esc(k)}</td><td style="padding:10px 14px;border-top:1px solid #f1f5f9;color:#1e293b;font-size:14px;word-break:break-word">${esc(v)}</td></tr>`;
|
|
const body = title('New sign-in', `Someone signed in to your ${b.name} account`)
|
|
+ hello(o.name)
|
|
+ `<table role="presentation" width="100%" cellpadding="0" cellspacing="0" style="border:1px solid #e2e8f0;border-radius:8px;overflow:hidden;margin:0 0 8px 0">`
|
|
+ row('When', o.when) + (o.site ? row('Signed in to', o.site) : '') + row('Where (roughly)', o.location) + row('Device', o.device)
|
|
+ '</table>'
|
|
+ callout(`<strong>Not you?</strong> Change your password straight away from your account page, and let the church office know${contactLine(b)}.`, 'warning')
|
|
+ small('If this was you, there\'s nothing to do.');
|
|
const lines = [`When: ${o.when}`, ...(o.site ? [`Signed in to: ${o.site}`] : []), `Where (roughly): ${o.location}`, `Device: ${o.device}`];
|
|
const text = `Hello ${o.name || 'there'},\n\nSomeone signed in to your ${b.name} account.\n\n${lines.join('\n')}\n\nIf this wasn't you, change your password straight away and let the church office know${b.email ? ` (${b.email})` : ''}.\nIf it was you, there's nothing to do.`;
|
|
const whatsapp = `🔐 *New sign-in*\n\nHello ${o.name || 'there'}, someone signed in to your *${b.name}* account.\n\n${lines.join('\n')}\n\nNot you? Change your password straight away and let the church office know.`;
|
|
return { subject: `New sign-in to your ${b.name} account`, text, html: layout(b, body, `New sign-in: ${o.when}, ${o.device}.`), whatsapp };
|
|
}
|