"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"; import { AccountsMessaging } from "@/components/admin/AccountsMessaging"; 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(null); const [checked, setChecked] = useState(null); const [admins, setAdmins] = useState>(new Set()); const [keepAccountsNotices, setKeepAccountsNotices] = useState(false); const [busy, setBusy] = useState(null); const [msg, setMsg] = useState<{ ok: boolean; text: string } | null>(null); // 3.1.0: what Accounts can send (the move needs its email). const [sending, setSending] = useState<{ email: boolean; whatsapp: boolean } | 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 ?

{msg.text}

:

Loading…

; 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 (

Sign-in

{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."}

{msg &&

{msg.text}

} {status.accountsConnected && } {status.accountsConnected && (

How the sign-in page looks

The church's sign-in page uses its name, logo and colours. Moving sign-in copies this site's when it has none; this replaces them with this site's now.

)} {status.mode === "accounts" ? (

Through Accounts{status.accountsUrl ? <> at {status.accountsUrl.replace(/^https?:\/\//, "")} : null}{status.movedAt ? `, since ${when(status.movedAt)}` : ""}.

{report && (
  • {report.created} people moved{report.by ? ` (by ${report.by})` : ""}.
  • {report.linked.length > 0 &&
  • {report.linked.length} already had an account and were linked to it: {report.linked.map((l) => l.email).join(", ")}.
  • } {report.refused.length > 0 &&
  • {report.refused.length} couldn't move: {report.refused.map((r) => `${r.email} (${r.reason})`).join("; ")}.
  • }
  • {report.skipped} guests and deleted people stay in Cross Events only.
  • Also Accounts admins: {report.accountsAdmins.map((a) => a.name).join(", ") || "nobody"}.
)} {status.undoUntil && !status.secretsCleared ? (
Possible until {when(status.undoUntil)}. After that, Cross Events removes the old passwords.
) :

Cross Events no longer holds anyone's password.

}
) : !status.accountsConnected ? (

Connect your church's Accounts first, under Linked products. Ask Cross Code to install Accounts if it isn't listed there.

) : (
Nothing changes until you press Move.
{checked && (
  • {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)` : ""}.
  • {checked.wouldLink.length > 0 &&
  • {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(", ")}.
  • } {checked.refused.length > 0 &&
  • {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.
  • }
  • {checked.skipped} guests and deleted people stay in Cross Events only.
  • Accounts {checked.accountsVersion}.
{checked.admins.length > 0 && (
Also make these admins Accounts admins (they can manage everyone's sign-in) {checked.admins.map((a) => ( ))}
)} {checked.noticeConflict && (
Accounts already has its own privacy notice. Which one should people see?
)} {!(sending?.email ?? checked.messaging.email) && (

Set up Accounts' email above first (copy this site's, test it, save it): without it nobody could get a sign-in code or password link after the move.

)} {!!checked.messaging.whatsappTwoFactor && !(sending?.whatsapp ?? checked.messaging.whatsapp) && (

{checked.messaging.whatsappTwoFactor} people get their codes by WhatsApp, which Accounts can't send yet: their codes will go by email until you set it up.

)}

Pick a quiet time. It takes about a minute; people already signed in stay signed in. You can undo it for 14 days.

)}
)}
); }