feat(website): crosscode website.sh and signing in through Accounts
crosscode: scripts/products/website.sh (ports 5300/3300, uploads in the export), crosscode add <church> website (needs Accounts; www.<address> or the Cross Code address), one install_app for Accounts and the Website, env template copy, bats tests. Backend: Hub client (protocol v2, product website), crosscode.link/1 with automatic connect to the church's Accounts, sign-in through @crosscode/auth-client with the Website's own session and roles, the first Owner from HUB_ADMIN_EMAIL (verified), 2FA for Owners and Admins, back-channel logout, Accounts' user.* calls, and the church export. Frontend: admin sign-in/out and the sign-in problem page. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,13 +1,26 @@
|
||||
import type { Metadata } from "next";
|
||||
import { redirect } from "next/navigation";
|
||||
import { loadMe, ROLE_NAMES, signInHref } from "@/lib/admin";
|
||||
|
||||
export const metadata: Metadata = { title: "Admin", robots: { index: false, follow: false } };
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
// The admin (docs/WEBSITE-1.0-PLAN.md §10), signed in through the church's Accounts, comes next.
|
||||
export default function Admin() {
|
||||
// The admin (docs/WEBSITE-1.0-PLAN.md §10), signed in through the church's Accounts. The screens
|
||||
// (template and style, pages, messages, events, publish) come next.
|
||||
export default async function Admin() {
|
||||
const me = await loadMe();
|
||||
if (!me) redirect(signInHref("/admin"));
|
||||
return (
|
||||
<main className="mx-auto max-w-xl px-4 py-16">
|
||||
<h1 className="text-2xl font-semibold">Website admin</h1>
|
||||
<p className="mt-2 text-gray-600">Not built yet.</p>
|
||||
<p className="mt-2 text-gray-600">
|
||||
Signed in as {me.user.name} ({me.user.email}), {ROLE_NAMES[me.user.role]}.
|
||||
</p>
|
||||
<form method="post" action="/api/auth/signout" className="mt-6">
|
||||
<button type="submit" className="rounded border border-gray-300 px-4 py-2 text-sm hover:bg-gray-50">
|
||||
Sign out
|
||||
</button>
|
||||
</form>
|
||||
</main>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
import type { Metadata } from "next";
|
||||
import Link from "next/link";
|
||||
import { signInHref } from "@/lib/admin";
|
||||
|
||||
export const metadata: Metadata = { title: "Signing in", robots: { index: false, follow: false } };
|
||||
|
||||
/** What went wrong with a sign-in (the backend's auth/routes.ts sends people here with a code). */
|
||||
const PROBLEMS: Record<string, { title: string; text: string; retry: boolean }> = {
|
||||
NO_ACCESS: {
|
||||
title: "No access to this website's admin",
|
||||
text: "You're signed in, but your account hasn't been given a role on this website. Ask the website's Owner to add you, then try again.",
|
||||
retry: true,
|
||||
},
|
||||
TWO_FACTOR_NEEDED: {
|
||||
title: "Two-step sign-in needed",
|
||||
text: "Owners and Admins of this website sign in with a second step. Turn on two-step sign-in in your church account, then try again.",
|
||||
retry: true,
|
||||
},
|
||||
ACCOUNTS_UNAVAILABLE: {
|
||||
title: "Signing in isn't available right now",
|
||||
text: "The website can't reach your church's sign-in at the moment. Please try again in a few minutes.",
|
||||
retry: true,
|
||||
},
|
||||
EXPIRED: { title: "The sign-in took too long", text: "Please try again.", retry: true },
|
||||
SIGNIN_FAILED: { title: "That didn't work", text: "Something went wrong while signing you in. Please try again.", retry: true },
|
||||
};
|
||||
|
||||
export default async function Problem({ searchParams }: { searchParams: Promise<Record<string, string | string[] | undefined>> }) {
|
||||
const q = await searchParams;
|
||||
const code = typeof q.code === "string" ? q.code : "";
|
||||
const returnTo = typeof q.returnTo === "string" && q.returnTo.startsWith("/") && !q.returnTo.startsWith("//") ? q.returnTo : "/admin";
|
||||
const p = PROBLEMS[code] ?? PROBLEMS.SIGNIN_FAILED!;
|
||||
return (
|
||||
<main className="mx-auto max-w-xl px-4 py-16">
|
||||
<h1 className="text-2xl font-semibold">{p.title}</h1>
|
||||
<p className="mt-2 text-gray-600">{p.text}</p>
|
||||
<div className="mt-6 flex gap-3">
|
||||
{p.retry && (
|
||||
<a href={signInHref(returnTo)} className="rounded bg-brand px-4 py-2 text-sm text-white hover:bg-brand-dark">
|
||||
Try again
|
||||
</a>
|
||||
)}
|
||||
<Link href="/" className="rounded border border-gray-300 px-4 py-2 text-sm hover:bg-gray-50">
|
||||
Go to the website
|
||||
</Link>
|
||||
</div>
|
||||
</main>
|
||||
);
|
||||
}
|
||||
@@ -3,5 +3,5 @@ import type { MetadataRoute } from "next";
|
||||
// The public site is indexed; the admin, previews and API never are (ROADMAP-v3 §11). The sitemap
|
||||
// comes with the published pages.
|
||||
export default function robots(): MetadataRoute.Robots {
|
||||
return { rules: [{ userAgent: "*", allow: "/", disallow: ["/admin", "/preview", "/api"] }] };
|
||||
return { rules: [{ userAgent: "*", allow: "/", disallow: ["/admin", "/preview", "/auth", "/api"] }] };
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
import { cookies } from "next/headers";
|
||||
|
||||
/** The signed-in admin (from the backend's /api/auth/me), for the admin's server-rendered pages. */
|
||||
export type Me = {
|
||||
user: { id: string; name: string; email: string; role: "owner" | "admin" | "editor" | "ministry_leader" | "viewer"; ministryId: string | null };
|
||||
mfa: boolean;
|
||||
};
|
||||
|
||||
const api = (process.env.WEBSITE_API_URL || "http://127.0.0.1:5300").replace(/\/+$/, "");
|
||||
|
||||
export const ROLE_NAMES: Record<Me["user"]["role"], string> = {
|
||||
owner: "Owner",
|
||||
admin: "Admin",
|
||||
editor: "Editor",
|
||||
ministry_leader: "Ministry leader",
|
||||
viewer: "Viewer",
|
||||
};
|
||||
|
||||
/** Null when signed out (or the backend can't say); the page then sends people to sign in. */
|
||||
export async function loadMe(): Promise<Me | null> {
|
||||
const session = (await cookies()).get("ccw_session")?.value;
|
||||
if (!session) return null;
|
||||
try {
|
||||
const res = await fetch(`${api}/api/auth/me`, { headers: { Cookie: `ccw_session=${encodeURIComponent(session)}` }, cache: "no-store" });
|
||||
if (res.ok) return (await res.json()) as Me;
|
||||
} catch {
|
||||
// The backend is starting or down: treated as signed out.
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Where "Sign in" goes for a page of the admin: Accounts, and back to that page. */
|
||||
export const signInHref = (returnTo: string) => `/api/auth/signin?returnTo=${encodeURIComponent(returnTo)}`;
|
||||
Reference in New Issue
Block a user