This repository demonstrates a static site which is hosted on Cloudflare Pages and whose content is "protected" by a shared username/password with HTTP basic access authentication. The real magic is in functions/_middleware.ts
, which is based on the Cloudflare Workers example for HTTP basic authentication, repurposed as a middleware to be used with the Functions feature of Cloudflare Pages.
This demonstration site is hosted at https://cloudflare-pages-shared-password.pages.dev/, with admin
as both the username and password.
The static site in this repository is built using Hugo, but the middleware can be used with any static site hosted on Cloudflare Pages.
I make no guarantees about the security of this code. In particular, a shared password, stored in a repository, should not be expected to provide a meaningful degree of security.
Special thanks to CherryJimbo and Isaac McFadyen for responding to my questions on the Cloudflare Discord instance. Their suggestions were essential in getting the first version of this code to work.