/* ═══════════════════════════════════════════════════════════════ INVITES: how a profile account (076) comes to exist Nobody signs up. An admin adds an email (Data → Profile accounts, routes/profiles.js), which makes a member account with no password, and issues an invite: a link to /account/welcome with a random token. The token itself is shown once, to the admin, and emailed if they press Email invite; the database keeps only its SHA-256, like a session's. Issuing a new one replaces the old, so a link sent to the wrong place can be killed by sending another. The link lets its holder set a password (routes/account.js). Signing in with Google under the invited email takes it up too (routes/auth.js). Either way the token is cleared. ═══════════════════════════════════════════════════════════════ */ import { createHash, randomBytes } from "node:crypto"; import { loadSite } from "./pageMeta.js"; import { mailReady, sendMail } from "./googleService.js"; export const INVITE_DAYS = 14; const sha256 = (value) => createHash("sha256").update(value).digest("hex"); /* A new invite for this account, replacing any before it. Returns the link, which is never stored. */ export function issueInvite(db, accountId, invitedBy) { const token = randomBytes(32).toString("base64url"); db.prepare( `UPDATE admin_users SET invite_hash = ?, invite_expires_at = datetime('now', ?), invited_at = datetime('now'), invited_by = ? WHERE id = ?`, ).run(sha256(token), `+${INVITE_DAYS} days`, invitedBy, accountId); return inviteLink(token); } export function inviteLink(token) { let origin = ""; try { origin = (loadSite().url ?? "").replace(/\/$/, ""); } catch { origin = ""; } return `${origin}/account/welcome?token=${encodeURIComponent(token)}`; } /* The account an unexpired invite token belongs to, or null. */ export function accountForInvite(db, token) { if (typeof token !== "string" || token.length < 20 || token.length > 100) return null; return ( db .prepare( `SELECT id, email, name, role, is_active FROM admin_users WHERE invite_hash = ? AND invite_expires_at > datetime('now') AND is_active = 1`, ) .get(sha256(token)) ?? null ); } /* The invite as an email, through Gmail (googleService.js). */ export async function emailInvite(db, account, link) { if (!mailReady()) throw new Error("Gmail isn't set up on the server (docs/google-drive-and-mail.md)."); let site = { title: "Next Generation of Unity" }; try { site = loadSite(); } catch { // The default name will do. } const hello = account.name ? `Hi ${account.name},` : "Hi,"; await sendMail({ to: account.email, subject: `Your ${site.title} profile`, text: [ hello, "", `You're invited to make a profile account on the ${site.title} website. Open this link to set a password, or to sign in with Google under this email:`, "", link, "", `The link works once, for ${INVITE_DAYS} days. Once you're in, you can find your profile on the site, ask to claim it, and choose what of it shows.`, "", "If you weren't expecting this, you can ignore it.", ].join("\n"), }); }