|
| 1 | +import { NextRequest, NextResponse } from 'next/server'; |
| 2 | + |
| 3 | +// TEMPORARY site-wide password gate. |
| 4 | +// |
| 5 | +// Active only when SITE_PASSWORD is set. Set it in Vercel's Production |
| 6 | +// environment so the gate shows on production only; leave it unset for local |
| 7 | +// dev and preview. The password itself is never committed — it lives in the |
| 8 | +// env var. |
| 9 | +// |
| 10 | +// The gate covers UI pages only. /api/* is intentionally left public so the |
| 11 | +// snapshots API stays reachable by external consumers. |
| 12 | +// |
| 13 | +// To remove the gate later: delete this file and app/api/gate/route.ts, and |
| 14 | +// unset SITE_PASSWORD in Vercel. |
| 15 | + |
| 16 | +const COOKIE = 'site_gate'; |
| 17 | + |
| 18 | +export function middleware(req: NextRequest) { |
| 19 | + const password = process.env.SITE_PASSWORD; |
| 20 | + |
| 21 | + // No password configured (local dev / preview) -> no gate. |
| 22 | + if (!password || process.env.NODE_ENV === 'development') { |
| 23 | + return NextResponse.next(); |
| 24 | + } |
| 25 | + |
| 26 | + if (req.cookies.get(COOKIE)?.value === password) { |
| 27 | + return NextResponse.next(); |
| 28 | + } |
| 29 | + |
| 30 | + return new NextResponse(gateHtml(), { |
| 31 | + status: 401, |
| 32 | + headers: { |
| 33 | + 'content-type': 'text/html; charset=utf-8', |
| 34 | + 'cache-control': 'no-store', |
| 35 | + }, |
| 36 | + }); |
| 37 | +} |
| 38 | + |
| 39 | +// Match everything except the public API, Next internals, and static assets. |
| 40 | +export const config = { |
| 41 | + matcher: ['/((?!api/|_next/static|_next/image|favicon.ico|robots.txt|sitemap.xml).*)'], |
| 42 | +}; |
| 43 | + |
| 44 | +// Self-contained gate screen (no app layout / external assets), posts the |
| 45 | +// password to /api/gate and reloads on success. |
| 46 | +function gateHtml(): string { |
| 47 | + return `<!doctype html> |
| 48 | +<html lang="en"> |
| 49 | +<head> |
| 50 | +<meta charset="utf-8" /> |
| 51 | +<meta name="viewport" content="width=device-width, initial-scale=1" /> |
| 52 | +<meta name="robots" content="noindex" /> |
| 53 | +<title>Base Labs</title> |
| 54 | +<style> |
| 55 | + :root { color-scheme: light; } |
| 56 | + * { box-sizing: border-box; } |
| 57 | + body { margin:0; min-height:100vh; display:flex; align-items:center; justify-content:center; |
| 58 | + font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif; background:#0000ff; } |
| 59 | + .card { background:#fff; padding:32px; border-radius:14px; width:100%; max-width:340px; |
| 60 | + box-shadow:0 10px 40px rgba(0,0,0,.25); } |
| 61 | + h1 { font-size:16px; margin:0 0 4px; color:#111; } |
| 62 | + p { font-size:13px; color:#666; margin:0 0 20px; } |
| 63 | + input { width:100%; padding:10px 12px; font-size:14px; border:1px solid #ddd; border-radius:8px; |
| 64 | + outline:none; color:#111; } |
| 65 | + input:focus { border-color:#0000ff; } |
| 66 | + button { width:100%; margin-top:12px; padding:10px 12px; font-size:14px; font-weight:500; |
| 67 | + color:#fff; background:#0000ff; border:none; border-radius:8px; cursor:pointer; } |
| 68 | + .err { color:#c00; font-size:12px; margin-top:10px; min-height:16px; } |
| 69 | +</style> |
| 70 | +</head> |
| 71 | +<body> |
| 72 | + <form class="card" id="gate"> |
| 73 | + <h1>Base Labs</h1> |
| 74 | + <p>This site is password protected.</p> |
| 75 | + <input id="pw" type="password" placeholder="Password" autocomplete="current-password" autofocus /> |
| 76 | + <button type="submit">Enter</button> |
| 77 | + <div class="err" id="err"></div> |
| 78 | + </form> |
| 79 | + <script> |
| 80 | + var f = document.getElementById('gate'); |
| 81 | + var pw = document.getElementById('pw'); |
| 82 | + var err = document.getElementById('err'); |
| 83 | + f.addEventListener('submit', function (ev) { |
| 84 | + ev.preventDefault(); |
| 85 | + err.textContent = ''; |
| 86 | + fetch('/api/gate', { |
| 87 | + method: 'POST', |
| 88 | + headers: { 'content-type': 'application/json' }, |
| 89 | + body: JSON.stringify({ password: pw.value }), |
| 90 | + }).then(function (r) { |
| 91 | + if (r.ok) { location.reload(); } |
| 92 | + else { err.textContent = 'Incorrect password'; pw.value = ''; pw.focus(); } |
| 93 | + }).catch(function () { err.textContent = 'Something went wrong. Try again.'; }); |
| 94 | + }); |
| 95 | + </script> |
| 96 | +</body> |
| 97 | +</html>`; |
| 98 | +} |
0 commit comments