The form is built once to the settled field set, and a valid send is saved before anyone is emailed. Co-authored-by: Cursor <cursoragent@cursor.com>
40 lines
1.3 KiB
TypeScript
40 lines
1.3 KiB
TypeScript
import { cookies } from "next/headers";
|
|
import { sha256Hex } from "./hash";
|
|
|
|
export const WORKSHOP_INQUIRY_ADMIN_COOKIE = "workshop_inquiry_admin";
|
|
|
|
export function workshopInquiryAdminToken(): string | null {
|
|
const raw = process.env.WORKSHOP_INQUIRY_ADMIN_TOKEN?.trim();
|
|
return raw && raw.length > 0 ? raw : null;
|
|
}
|
|
|
|
export function workshopInquiryPurgeToken(): string | null {
|
|
const raw = process.env.WORKSHOP_INQUIRY_PURGE_TOKEN?.trim();
|
|
return raw && raw.length > 0 ? raw : null;
|
|
}
|
|
|
|
function cookieValue(token: string): string {
|
|
return sha256Hex(`workshop-inquiry-admin:${token}`);
|
|
}
|
|
|
|
export async function isWorkshopInquiryAdmin(): Promise<boolean> {
|
|
const token = workshopInquiryAdminToken();
|
|
if (!token) return false;
|
|
const store = await cookies();
|
|
return store.get(WORKSHOP_INQUIRY_ADMIN_COOKIE)?.value === cookieValue(token);
|
|
}
|
|
|
|
export async function grantWorkshopInquiryAdmin(password: string): Promise<boolean> {
|
|
const token = workshopInquiryAdminToken();
|
|
if (!token || password !== token) return false;
|
|
const store = await cookies();
|
|
store.set(WORKSHOP_INQUIRY_ADMIN_COOKIE, cookieValue(token), {
|
|
httpOnly: true,
|
|
sameSite: "lax",
|
|
secure: process.env.NODE_ENV === "production",
|
|
path: "/",
|
|
maxAge: 60 * 60 * 12,
|
|
});
|
|
return true;
|
|
}
|