g1t/apps/web/app/lib/registration.server.ts

30 lines1,097 bytesCodeBlame
1import type { RegistrationMode } from "@g1t/contracts";
2
3import { identity } from "./services.server";
4
5/** How long one isolate trusts the answer before asking identity again. */
6const TTL_MS = 60_000;
7
8let known: { mode: RegistrationMode; at: number } | null = null;
9
10/**
11 * Whether g1t.sh is invite-only, as identity's REGISTRATION_MODE says.
12 * Asked at most once a minute per isolate; when identity cannot answer,
13 * the site says invite-only, the same as identity does with no setting.
14 */
15export async function registrationMode(): Promise<RegistrationMode> {
16 const now = Date.now();
17 if (known && now - known.at < TTL_MS) return known.mode;
18 try {
19 const mode = await identity.registration();
20 known = { mode: mode === "open" ? "open" : "invite", at: now };
21 return known.mode;
22 } catch {
23 return "invite";
24 }
25}
26
27/** Who is asking, for identity's rate limits: the visitor's address. */
28export function clientKey(request: Request): string | null {
29 return request.headers.get("cf-connecting-ip") ?? request.headers.get("x-forwarded-for")?.split(",")[0]?.trim() ?? null;
30}