Files
pavel444-byte 50b27cf498
All checks were successful
Deploy MineGeo to Dokploy / deploy (push) Successful in 3m27s
Add moderator bots and editable MineGeo usernames
2026-10-29 16:32:55 +05:00

33 lines
2.6 KiB
TypeScript

import { createHash } from "node:crypto";
import { createClient } from "@supabase/supabase-js";
import { redirect } from "next/navigation";
import { user } from "@/lib/supabase";
import { Header, Footer } from "@/components/Shell";
export const metadata = { robots: { index: false, follow: false }, referrer: "no-referrer" as const };
function db() {
return createClient(process.env.NEXT_PUBLIC_SUPABASE_URL!, process.env.SUPABASE_SECRET_KEY!, { auth: { persistSession: false } });
}
async function confirm(form: FormData) {
"use server";
const token = String(form.get("token") ?? "");
if (!/^[a-f0-9]{64}$/.test(token)) throw new Error("Invalid application link");
const account = await user();
if (!account) redirect(`/log-in?next=${encodeURIComponent(`/moderator-application/${token}`)}`);
const database = db();
const { data: profile, error: profileError } = await database.from("profiles").select("username,account_status").eq("id", account.id).single();
if (profileError || !profile || profile.account_status !== "active" || !profile.username) redirect(`/moderator-application/${token}?error=username`);
const hash = createHash("sha256").update(token).digest("hex");
const { data, error } = await database.from("moderator_applications").update({ status: "pending", profile_id: account.id }).eq("confirmation_hash", hash).eq("requested_username", profile.username).eq("status", "confirming").gt("expires_at", new Date().toISOString()).select("id").maybeSingle();
if (error || !data) redirect(`/moderator-application/${token}?error=link`);
redirect(`/moderator-application/${token}?submitted=1`);
}
export default async function ApplicationPage({ params, searchParams }: { params: Promise<{ token: string }>; searchParams: Promise<{ submitted?: string; error?: string }> }) {
const { token } = await params;
if (!/^[a-f0-9]{64}$/.test(token)) return <main>Invalid application link.</main>;
const account = await user();
if (!account) redirect(`/log-in?next=${encodeURIComponent(`/moderator-application/${token}`)}`);
const { submitted, error } = await searchParams;
return <><Header /><main className="section page-content"><h1>Moderator application</h1>{submitted ? <p>Application sent for review.</p> : <><p>Confirm that this MineGeo account belongs to you.</p>{error && <p role="alert">{error === "username" ? "Set your username in account settings first." : "This link expired, was already used, or belongs to a different username. Start again in the bot."}</p>}<form action={confirm}><input type="hidden" name="token" value={token} /><button className="button primary">Confirm application</button></form></>}</main><Footer /></>;
}