Password Protecting a Netlify Site Without Basic Auth

Nov 22, 2026

When I build a demo website for a business, it carries their name before they've agreed to anything. So it lives behind a password until they've seen it and said yes.

My first attempt was HTTP Basic Auth, the browser's built-in username and password prompt. On an iPhone, Safari never showed the login dialog at all. I got the 401 error body as plain text with nowhere to type a password. On browsers where the dialog did show up, the visitor got an unbranded browser popup asking for credentials, which, coming from a stranger's email, looks a lot like phishing.

Goal

A password page that works on every phone, says who built the site, and stays locked if anything is misconfigured.

The Edge Function

Netlify Edge Functions run before every request, which makes them a good fit for a gate. Save this as netlify/edge-functions/password-gate.ts:

import type { Config } from "@netlify/edge-functions";

const COOKIE = "preview";

async function hash(value: string) {
  const data = new TextEncoder().encode("my-site:" + value);
  const digest = await crypto.subtle.digest("SHA-256", data);
  return [...new Uint8Array(digest)]
    .map((b) => b.toString(16).padStart(2, "0"))
    .join("");
}

export default async (request: Request) => {
  const password = Netlify.env.get("SITE_PASSWORD");
  if (!password) return lockScreen("This site is unavailable.");

  const expected = await hash(password);
  const url = new URL(request.url);

  if (request.method === "POST" && url.pathname === "/__unlock") {
    const form = await request.formData();
    if ((await hash(String(form.get("password") ?? ""))) !== expected) {
      return lockScreen("Wrong password, try again.");
    }
    return new Response(null, {
      status: 303,
      headers: {
        Location: "/",
        "Set-Cookie": `${COOKIE}=${expected}; Path=/; HttpOnly; Secure; SameSite=Lax; Max-Age=2592000`,
      },
    });
  }

  const cookies = (request.headers.get("Cookie") ?? "").split(/;\s*/);
  if (cookies.includes(`${COOKIE}=${expected}`)) return; // unlocked, carry on

  return lockScreen();
};

function lockScreen(message = "") {
  return new Response(
    `<!doctype html><meta name="viewport" content="width=device-width">
    <h1>Preview by Mural City Web Design</h1>
    <p>${message}</p>
    <form method="post" action="/__unlock">
      <input type="password" name="password" autofocus>
      <button>Enter</button>
    </form>`,
    {
      status: 401,
      headers: { "Content-Type": "text/html; charset=utf-8", "X-Robots-Tag": "noindex" },
    }
  );
}

export const config: Config = { path: "/*" };

Then add SITE_PASSWORD under Site configuration → Environment variables in Netlify.

If SITE_PASSWORD isn't set, every page shows the lock screen and nothing can unlock it. I'd rather a mistake hide the site than expose it. The cookie stores a hash of the password, not the password itself, and the my-site: prefix means that hash isn't reusable anywhere else. And since the lock screen is a normal HTML form, it works anywhere a web page works, iPhones included, with your name on it instead of a browser popup.

(My real version also uses a constant-time comparison and pings Slack when someone gets in, but this is the core of it.)

Basic Auth is fine for a staging site only you will ever see. For anything a real person will open on their phone, the form is worth the extra fifty lines.

Thanks for reading! I'd love to hear your thoughts - if you have something you want to share feel free to leave a comment or shoot me an email.

Comments

Leave a Comment