mail-server serves apps/mail/dashboard at /dashboard/ (tower-http fs,
DASHBOARD_DIR, mock/ and MOUNT.md never served). New account-scoped,
Resend-shaped endpoints the dashboard reads: GET /emails/{id}/events
(status history with timestamps and a one-line detail from the stored
payload), GET /emails/{id}/webhook-deliveries and
GET /webhooks/{id}/deliveries (delivery log joined with event + endpoint),
GET /emails?test=true|false and GET /suppressions?q= server filters.
GET /billing now carries description, next_plan, upgrade_label,
current_period_end/renews_at; POST /billing/checkout accepts success_url
(own origin only) and returns url. mail-auth: a bad, used or expired link
opened in a browser redirects to <redirect_to>#error=invalid_token (or a
plain HTML page when the token was never ours); the dashboard shows the
message on the login form. Dashboard uses the server-side test filter,
portal via POST /billing/portal, docs updated.
Verified live against local Postgres mail_e2e + fake relay + webhook
listener: curl of every endpoint (404 for another account's ids, 401
without auth, 422 on test=maybe), and a headless-browser pass through the
real magic-link login, email timeline, webhook delivery log, usage card
and the expired-link redirect.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
107 lines
3.3 KiB
JavaScript
107 lines
3.3 KiB
JavaScript
// Session: the mail-auth JWT, the selected account, and the live/test switch.
|
|
// Nothing here talks to the network.
|
|
|
|
const KEY_SESSION = "sm.session";
|
|
const KEY_ACCOUNT = "sm.account";
|
|
const KEY_MODE = "sm.mode";
|
|
const KEY_ONBOARDED = "sm.onboarded.";
|
|
|
|
const listeners = new Set();
|
|
const notify = () => listeners.forEach((fn) => fn());
|
|
|
|
export const onChange = (fn) => {
|
|
listeners.add(fn);
|
|
return () => listeners.delete(fn);
|
|
};
|
|
|
|
const read = (key) => {
|
|
try {
|
|
return JSON.parse(localStorage.getItem(key));
|
|
} catch {
|
|
return null;
|
|
}
|
|
};
|
|
const write = (key, value) =>
|
|
value === null || value === undefined
|
|
? localStorage.removeItem(key)
|
|
: localStorage.setItem(key, JSON.stringify(value));
|
|
|
|
/** Decode a JWT payload without verifying (the server verifies). */
|
|
export const decodeJwt = (token) => {
|
|
try {
|
|
const part = token.split(".")[1].replace(/-/g, "+").replace(/_/g, "/");
|
|
return JSON.parse(atob(part.padEnd(part.length + ((4 - (part.length % 4)) % 4), "=")));
|
|
} catch {
|
|
return {};
|
|
}
|
|
};
|
|
|
|
export const session = () => {
|
|
const s = read(KEY_SESSION);
|
|
if (!s || !s.token) return null;
|
|
if (s.exp && Date.now() / 1000 > s.exp) {
|
|
write(KEY_SESSION, null);
|
|
return null;
|
|
}
|
|
return s;
|
|
};
|
|
|
|
export const token = () => session()?.token || null;
|
|
export const userEmail = () => session()?.email || "";
|
|
|
|
export const signIn = (accessToken, expiresIn) => {
|
|
const claims = decodeJwt(accessToken);
|
|
const exp = claims.exp || Math.floor(Date.now() / 1000) + Number(expiresIn || 3600);
|
|
write(KEY_SESSION, { token: accessToken, exp, email: claims.email || "", sub: claims.sub || "" });
|
|
notify();
|
|
};
|
|
|
|
export const signOut = () => {
|
|
write(KEY_SESSION, null);
|
|
write(KEY_ACCOUNT, null);
|
|
notify();
|
|
};
|
|
|
|
/**
|
|
* mail-auth redirects back to `redirect_to#access_token=...&expires_in=...`.
|
|
* Consume that fragment once, replace it so the token never stays in the URL.
|
|
*/
|
|
export const consumeLoginFragment = () => {
|
|
const hash = location.hash.startsWith("#") ? location.hash.slice(1) : location.hash;
|
|
if (!hash.includes("access_token=")) return false;
|
|
const params = new URLSearchParams(hash);
|
|
const accessToken = params.get("access_token");
|
|
if (!accessToken) return false;
|
|
signIn(accessToken, params.get("expires_in"));
|
|
history.replaceState(null, "", location.pathname + location.search + "#/");
|
|
return true;
|
|
};
|
|
|
|
/**
|
|
* A bad or used magic link comes back as `redirect_to#error=invalid_token`.
|
|
* Consume it once (strip it from the URL) and return the code, or null.
|
|
*/
|
|
export const consumeErrorFragment = () => {
|
|
const hash = location.hash.startsWith("#") ? location.hash.slice(1) : location.hash;
|
|
if (!hash.startsWith("error=") && !hash.includes("&error=")) return null;
|
|
const code = new URLSearchParams(hash).get("error");
|
|
if (!code) return null;
|
|
history.replaceState(null, "", location.pathname + location.search + "#/");
|
|
return code;
|
|
};
|
|
|
|
export const accountId = () => read(KEY_ACCOUNT);
|
|
export const setAccountId = (id) => {
|
|
write(KEY_ACCOUNT, id);
|
|
notify();
|
|
};
|
|
|
|
export const isTest = () => read(KEY_MODE) === "test";
|
|
export const setTest = (test) => {
|
|
write(KEY_MODE, test ? "test" : "live");
|
|
notify();
|
|
};
|
|
|
|
export const onboarded = (acct) => Boolean(read(KEY_ONBOARDED + acct));
|
|
export const setOnboarded = (acct) => write(KEY_ONBOARDED + acct, true);
|