Cross Events 3.0.0: sign-in through the church's Accounts
Settings -> Sign-in: check, move (everyone into Accounts with their password hashes and authenticator apps, count/hash check, then switch) and undo for 14 days. Sign-in through auth-client with a one-time handoff to Events' own token; staff roles stepped up to mfa; sign-out and back-channel logout; user.updated/export/deleted; admin 2FA reset, unlock and profile changes via Accounts; /login, /register, /forgot-password forward once moved; Events' own sign-in endpoints answer SIGN_IN_MOVED. User.accountId, AccountsSignIn. npm run test:accounts runs Events' code against the real Accounts app (11 tests); 5 new Jest tests. MIGRATING-TO-3.md, plan "Changes while building". Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,171 @@
|
||||
"use client";
|
||||
|
||||
import React, { useEffect, useState } from "react";
|
||||
import { LogIn } from "lucide-react";
|
||||
import { useAuth } from "@/hooks/useAuth";
|
||||
import { apiV2, type V2Result } from "@/lib/apiV2";
|
||||
import { errorMessage } from "@/lib/utils";
|
||||
|
||||
type Status = V2Result<"/settings/sign-in", "get">["data"];
|
||||
type Check = V2Result<"/settings/sign-in/check", "post">["data"];
|
||||
type Report = { created: number; linked: { name: string; email: string }[]; refused: { name: string; email: string; reason: string }[]; skipped: number; accountsAdmins: { name: string }[]; by: string; movedAt: string };
|
||||
|
||||
const when = (iso: string | null | undefined) =>
|
||||
iso ? new Date(iso).toLocaleString("en-ZA", { day: "numeric", month: "long", year: "numeric", hour: "2-digit", minute: "2-digit" }) : "";
|
||||
|
||||
/**
|
||||
* Site settings → Sign-in (3.0.0, docs/EVENTS-3.0-PLAN.md §5): where people sign in, and moving
|
||||
* sign-in to the church's Accounts: Check (nothing changes), Move (with which admins also manage
|
||||
* Accounts), and Undo for 14 days.
|
||||
*/
|
||||
export function SignInTab({ active }: { active: boolean }) {
|
||||
const { token } = useAuth();
|
||||
const [status, setStatus] = useState<Status | null>(null);
|
||||
const [checked, setChecked] = useState<Check | null>(null);
|
||||
const [admins, setAdmins] = useState<Set<string>>(new Set());
|
||||
const [keepAccountsNotices, setKeepAccountsNotices] = useState(false);
|
||||
const [busy, setBusy] = useState<string | null>(null);
|
||||
const [msg, setMsg] = useState<{ ok: boolean; text: string } | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (!active || !token) return;
|
||||
let alive = true;
|
||||
apiV2("get", "/settings/sign-in", { authToken: token })
|
||||
.then(({ data }) => { if (alive) setStatus(data); })
|
||||
.catch((e) => { if (alive) setMsg({ ok: false, text: errorMessage(e) || "Could not load the sign-in settings." }); });
|
||||
return () => { alive = false; };
|
||||
}, [active, token]);
|
||||
|
||||
if (!token) return null;
|
||||
if (!status) return msg ? <p className="text-sm text-red-700">{msg.text}</p> : <p className="text-sm text-gray-500">Loading…</p>;
|
||||
const auth = { authToken: token };
|
||||
|
||||
const runCheck = async () => {
|
||||
setBusy("check");
|
||||
setMsg(null);
|
||||
try {
|
||||
const { data } = await apiV2("post", "/settings/sign-in/check", auth);
|
||||
setChecked(data);
|
||||
setAdmins(new Set(data.admins.map((a) => a.id)));
|
||||
} catch (e) {
|
||||
setMsg({ ok: false, text: errorMessage(e) || "The check didn't work." });
|
||||
}
|
||||
setBusy(null);
|
||||
};
|
||||
|
||||
const runMove = async () => {
|
||||
if (!checked) return;
|
||||
if (!window.confirm(`Move sign-in to your church's Accounts now? ${checked.toMove} people will sign in there from now on, with the passwords they have today.`)) return;
|
||||
setBusy("move");
|
||||
setMsg(null);
|
||||
try {
|
||||
const { data } = await apiV2("post", "/settings/sign-in/move", { ...auth, body: { adminIds: [...admins], ...(checked.noticeConflict ? { keepAccountsNotices } : {}) } });
|
||||
setStatus(data);
|
||||
setChecked(null);
|
||||
setMsg({ ok: true, text: "Sign-in now goes through your church's Accounts. People already signed in stay signed in." });
|
||||
} catch (e) {
|
||||
setMsg({ ok: false, text: errorMessage(e) || "The move didn't finish. Nothing was switched; you can try again." });
|
||||
}
|
||||
setBusy(null);
|
||||
};
|
||||
|
||||
const runUndo = async () => {
|
||||
if (!window.confirm("Go back to Cross Events' own sign-in? People sign in with the password they had at the move. Changes made in Accounts since then (a new password, two-step sign-in) don't come back here.")) return;
|
||||
setBusy("undo");
|
||||
setMsg(null);
|
||||
try {
|
||||
const { data } = await apiV2("post", "/settings/sign-in/undo", auth);
|
||||
setStatus(data);
|
||||
setMsg({ ok: true, text: "Sign-in is back in Cross Events." });
|
||||
} catch (e) {
|
||||
setMsg({ ok: false, text: errorMessage(e) || "Undo didn't work." });
|
||||
}
|
||||
setBusy(null);
|
||||
};
|
||||
|
||||
const report = status.report as Report | null;
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div>
|
||||
<h2 className="flex items-center gap-2 text-lg font-semibold"><LogIn className="h-5 w-5 text-brand-600" aria-hidden /> Sign-in</h2>
|
||||
<p className="text-sm text-gray-600 mt-1">
|
||||
{status.mode === "accounts"
|
||||
? "People sign in with their church account, the same one they use for the church's other sites and apps. Passwords and two-step sign-in are managed there."
|
||||
: "People sign in with a Cross Events password. You can move sign-in to your church's Accounts, so one account works for all the church's sites and apps."}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{msg && <p className={`text-sm rounded-lg px-3 py-2 ${msg.ok ? "bg-green-50 text-green-800" : "bg-red-50 text-red-800"}`} role="status">{msg.text}</p>}
|
||||
|
||||
{status.mode === "accounts" ? (
|
||||
<section className="border rounded-xl p-4 space-y-3">
|
||||
<p className="text-sm"><span className="font-medium">Through Accounts</span>{status.accountsUrl ? <> at <a className="underline" href={status.accountsUrl} target="_blank" rel="noreferrer">{status.accountsUrl.replace(/^https?:\/\//, "")}</a></> : null}{status.movedAt ? `, since ${when(status.movedAt)}` : ""}.</p>
|
||||
{report && (
|
||||
<ul className="text-sm text-gray-700 list-disc pl-5 space-y-1">
|
||||
<li>{report.created} people moved{report.by ? ` (by ${report.by})` : ""}.</li>
|
||||
{report.linked.length > 0 && <li>{report.linked.length} already had an account and were linked to it: {report.linked.map((l) => l.email).join(", ")}.</li>}
|
||||
{report.refused.length > 0 && <li>{report.refused.length} couldn't move: {report.refused.map((r) => `${r.email} (${r.reason})`).join("; ")}.</li>}
|
||||
<li>{report.skipped} guests and deleted people stay in Cross Events only.</li>
|
||||
<li>Also Accounts admins: {report.accountsAdmins.map((a) => a.name).join(", ") || "nobody"}.</li>
|
||||
</ul>
|
||||
)}
|
||||
{status.undoUntil && !status.secretsCleared ? (
|
||||
<div className="flex flex-wrap items-center gap-3">
|
||||
<button type="button" onClick={runUndo} disabled={!!busy} className="px-3 py-2 rounded-lg border text-sm">{busy === "undo" ? "Going back…" : "Undo the move"}</button>
|
||||
<span className="text-xs text-gray-500">Possible until {when(status.undoUntil)}. After that, Cross Events removes the old passwords.</span>
|
||||
</div>
|
||||
) : <p className="text-xs text-gray-500">Cross Events no longer holds anyone's password.</p>}
|
||||
</section>
|
||||
) : !status.accountsConnected ? (
|
||||
<section className="border rounded-xl p-4">
|
||||
<p className="text-sm text-gray-700">Connect your church's Accounts first, under <span className="font-medium">Linked products</span>. Ask Cross Code to install Accounts if it isn't listed there.</p>
|
||||
</section>
|
||||
) : (
|
||||
<section className="border rounded-xl p-4 space-y-4">
|
||||
<div className="flex flex-wrap items-center gap-3">
|
||||
<button type="button" onClick={runCheck} disabled={!!busy || status.running} className="px-3 py-2 rounded-lg border text-sm">{busy === "check" ? "Checking…" : checked ? "Check again" : "Check the move"}</button>
|
||||
<span className="text-xs text-gray-500">Nothing changes until you press Move.</span>
|
||||
</div>
|
||||
{checked && (
|
||||
<div className="space-y-4">
|
||||
<ul className="text-sm text-gray-700 list-disc pl-5 space-y-1">
|
||||
<li>{checked.wouldCreate} people move with their current password{checked.withTwoFactor ? `; ${checked.withTwoFactor} use two-step sign-in and keep their method (they get new recovery codes the first time)` : ""}.</li>
|
||||
{checked.wouldLink.length > 0 && <li>{checked.wouldLink.length} already have a church account and will be linked to it (they keep that account's password): {checked.wouldLink.map((l) => l.email).join(", ")}.</li>}
|
||||
{checked.refused.length > 0 && <li className="text-red-700">{checked.refused.length} can't move: {checked.refused.map((r) => `${r.email} (${r.reason})`).join("; ")}. Fix these first, or they won't be able to sign in.</li>}
|
||||
<li>{checked.skipped} guests and deleted people stay in Cross Events only.</li>
|
||||
<li>Accounts {checked.accountsVersion}.</li>
|
||||
</ul>
|
||||
{checked.admins.length > 0 && (
|
||||
<fieldset>
|
||||
<legend className="text-sm font-medium mb-1">Also make these admins Accounts admins (they can manage everyone's sign-in)</legend>
|
||||
{checked.admins.map((a) => (
|
||||
<label key={a.id} className="flex items-center gap-2 text-sm py-0.5">
|
||||
<input type="checkbox" checked={admins.has(a.id)} onChange={(e) => {
|
||||
const next = new Set(admins);
|
||||
if (e.target.checked) next.add(a.id); else next.delete(a.id);
|
||||
setAdmins(next);
|
||||
}} />
|
||||
{a.name} <span className="text-gray-500">({a.email})</span>
|
||||
</label>
|
||||
))}
|
||||
</fieldset>
|
||||
)}
|
||||
{checked.noticeConflict && (
|
||||
<fieldset>
|
||||
<legend className="text-sm font-medium mb-1">Accounts already has its own privacy notice. Which one should people see?</legend>
|
||||
<label className="flex items-center gap-2 text-sm py-0.5"><input type="radio" name="notice" checked={keepAccountsNotices} onChange={() => setKeepAccountsNotices(true)} /> Keep Accounts' notice (everyone accepts it at their next sign-in)</label>
|
||||
<label className="flex items-center gap-2 text-sm py-0.5 text-gray-400"><input type="radio" name="notice" disabled checked={!keepAccountsNotices} readOnly /> Use Cross Events' notice (remove Accounts' notice in Accounts first)</label>
|
||||
</fieldset>
|
||||
)}
|
||||
<button type="button" onClick={runMove} disabled={!!busy || (!!checked.noticeConflict && !keepAccountsNotices)} className="px-4 py-2 rounded-lg bg-brand-600 text-white text-sm font-medium disabled:opacity-50">
|
||||
{busy === "move" ? "Moving… (don't close this page)" : "Move sign-in to Accounts"}
|
||||
</button>
|
||||
<p className="text-xs text-gray-500">Pick a quiet time. It takes about a minute; people already signed in stay signed in. You can undo it for 14 days.</p>
|
||||
</div>
|
||||
)}
|
||||
</section>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
"use client";
|
||||
|
||||
import React, { useEffect, useState } from "react";
|
||||
import { fetchAuthMode, safePath, signInHref, type AuthMode } from "@/lib/signIn";
|
||||
|
||||
type Kind = "login" | "register" | "forgot" | "reset" | "activate";
|
||||
|
||||
/**
|
||||
* Cross Events' own sign-in pages (3.0.0). With sign-in through Accounts, the sign-in, sign-up and
|
||||
* forgot-password pages forward there (keeping where to come back to), and old reset and activation
|
||||
* links say sign-in has moved. Otherwise (`local`, or `?local=1` from the backend) the page as before.
|
||||
*/
|
||||
export function AccountsGate({ kind, children }: { kind: Kind; children: React.ReactNode }) {
|
||||
const [mode, setMode] = useState<AuthMode | null>(null);
|
||||
useEffect(() => {
|
||||
let alive = true;
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
if (params.get("local") === "1") {
|
||||
setMode("local");
|
||||
return;
|
||||
}
|
||||
fetchAuthMode().then((m) => {
|
||||
if (!alive) return;
|
||||
if (m === "accounts" && (kind === "login" || kind === "register" || kind === "forgot")) {
|
||||
window.location.replace(signInHref(safePath(params.get("redirect")), { signUp: kind === "register" }));
|
||||
return;
|
||||
}
|
||||
setMode(m);
|
||||
});
|
||||
return () => { alive = false; };
|
||||
}, [kind]);
|
||||
|
||||
if (mode === null) {
|
||||
return <div className="min-h-[50vh] flex items-center justify-center text-sm text-gray-500" role="status">Loading…</div>;
|
||||
}
|
||||
if (mode === "accounts") return <SignInMoved kind={kind} />;
|
||||
return <>{children}</>;
|
||||
}
|
||||
|
||||
function SignInMoved({ kind }: { kind: Kind }) {
|
||||
return (
|
||||
<div className="min-h-[60vh] flex items-center justify-center p-6">
|
||||
<div className="w-full max-w-md border rounded-2xl p-8 bg-white shadow-sm">
|
||||
<h1 className="text-xl font-semibold text-gray-900 mb-2">Sign-in has moved</h1>
|
||||
<p className="text-sm text-gray-600 mb-6">
|
||||
{kind === "activate"
|
||||
? "You now sign in with your church account. To get started, use “Forgot your password?” there with your email address to choose a password."
|
||||
: "You now sign in with your church account, so this link no longer works. Use “Forgot your password?” there to choose a new one."}
|
||||
</p>
|
||||
<a href={signInHref("/dashboard")} className="inline-block w-full text-center px-4 py-2.5 rounded-lg bg-brand-600 text-white text-sm font-medium">Sign in</a>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
"use client";
|
||||
|
||||
import React, { useEffect, useState } from "react";
|
||||
import { fetchAuthInfo, type AuthInfo } from "@/lib/signIn";
|
||||
|
||||
/** Whether sign-in goes through the church's Accounts (3.0.0), and its account page; null while loading. */
|
||||
export function useAuthInfo(): AuthInfo | null {
|
||||
const [info, setInfo] = useState<AuthInfo | null>(null);
|
||||
useEffect(() => {
|
||||
let alive = true;
|
||||
fetchAuthInfo().then((i) => { if (alive) setInfo(i); });
|
||||
return () => { alive = false; };
|
||||
}, []);
|
||||
return info;
|
||||
}
|
||||
|
||||
/** "Your sign-in and security are on your church account", with a link to it. */
|
||||
export function ChurchAccountNotice({ accountUrl, what }: { accountUrl: string | null; what: string }) {
|
||||
return (
|
||||
<section className="border rounded-xl p-5 bg-white shadow-sm">
|
||||
<h2 className="text-lg font-semibold text-gray-900 mb-2">On your church account</h2>
|
||||
<p className="text-sm text-gray-600 mb-4">{what}</p>
|
||||
{accountUrl && (
|
||||
<a href={accountUrl} className="inline-block px-4 py-2 rounded-lg bg-brand-600 text-white text-sm font-medium hover:bg-brand-700">Open my church account</a>
|
||||
)}
|
||||
</section>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user