export const onRequest: PagesFunction<{ PASSWORD?: string }> = async (context) => { const { request, next, env } = context; const url = new URL(request.url); // 1. 설정된 비밀번호가 없으면 그냥 통과 (보안을 위해 대시보드 설정을 권장) const MASTER_PASSWORD = env.PASSWORD || "1234"; // 기본값 1234 // 2. 쿠키 확인 (이미 로그인했는지 체크) const cookie = request.headers.get("Cookie") || ""; if (cookie.includes(`site_auth=${MASTER_PASSWORD}`)) { return await next(); } // 3. 비밀번호 제출(POST) 처리 if (request.method === "POST") { const formData = await request.formData(); const enteredPassword = formData.get("password"); if (enteredPassword === MASTER_PASSWORD) { // 비밀번호 일치 시 쿠키 저장 후 리다이렉트 return new Response(null, { status: 302, headers: { "Location": url.pathname, "Set-Cookie": `site_auth=${MASTER_PASSWORD}; Path=/; HttpOnly; Secure; SameSite=Strict; Max-Age=604800`, // 7일 유지 }, }); } else { // 비밀번호 틀림 return new Response("비밀번호가 틀렸습니다.", { status: 401 }); } } // 4. 로그인 화면 출력 (HTML) return new Response( ` 인증 필요

접속을 위해
비밀번호를 입력하세요

`, { headers: { "Content-Type": "text/html" } } ); };