Code Monkey home page Code Monkey logo

cloudflare-pages-auth's Introduction

cloudflare-pages-auth

Static site that is password-protected when deployed to Cloudflare Pages. This is a SvelteKit project, but the password-protection works independently of the framework you use.

Screenshot

๐Ÿš€ Getting Started

If you want to password-protect your own Cloudflare Pages site, you'll need to:

  1. Copy the contents of the functions directory from this repo into your own project.
  2. Add a CFP_PASSWORD environment variable to your Cloudflare Pages dashboard with the password you want to use.

The next time you deploy your site, it will be password-protected! ๐ŸŽ‰

๐Ÿ›  Running Locally

Since this is a SvelteKit project, first run the build command to generate the static site:

npm run build

Then, you'll need to use the wrangler CLI to run the site (with functions) locally:

npx wrangler pages dev build -b CFP_PASSWORD=test

Notice that you'll need to pass the CFP_PASSWORD environment variable when running the CLI command. If you don't pass it, the site will not be password-protected.

๐Ÿ’… Customization

  • The functions/template.ts file contains the HTML template of the login page. You can customize it to your liking.
  • The functions/constants.ts contains some other things you can customize, like the expiration of the auth cookie, or the list of paths that don't require authentication.

cloudflare-pages-auth's People

Contributors

charca avatar renewerner87 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

cloudflare-pages-auth's Issues

Unable to view images after login when accessing directly

Description:

When trying to access an image directly (e.g., ogp.png), even after successfully logging in, I am redirected back to the login screen instead of being able to view the image. It seems like the login process isn't properly handling direct access to images or other static resources.

Steps to Reproduce:

  • Attempt to access ogp.png or any other image directly via URL.
  • Login when prompted.
  • Observe that you are redirected back to the login screen instead of viewing the image.

Expected Behavior: After logging in, I should be able to view the image directly.

Actual Behavior: After logging in, I'm redirected back to the login screen.

Additional Context: I want to enforce authentication for images but still allow access once logged in. However, the current behavior is preventing that.

Does this work with Vue PWA?

First, thank you very much for your work - this kind of password protection is really handy for most cases.

I used the vite-plugin-pwa to make my Vue App as PWA installable. That works locally really well.
But deployed on CF the web.manifest cannot be found correctly, because the new html section is injected and first shows the password prompt.

I already tried to copy the original html head into the injected one, but it does not seem to work.

Is there any way to accomplish this or do the two things (your auth and the pwa functionality) do not work together this way?

This is the PWA plugin I used: https://vite-pwa-org.netlify.app/guide/

Not working with SvelteKit and cloudflare adapter

Hello,

Thank you for your article and this code. I'm having a bit of a trouble getting it to work though. I'm using SvelteKit with the cloudflare adapter and I experience the following behaviour:

  • my first request gets correctly routed to the login page
  • if I enter wrong password, I get the error message displayed
  • if I enter correct password, I get 404 errors and no pages are found

My svelte.config.js looks like this:

import adapter from '@sveltejs/adapter-cloudflare';
import preprocess from 'svelte-preprocess';

/** @type {import('@sveltejs/kit').Config} */
const config = {
	// Consult https://github.com/sveltejs/svelte-preprocess
	// for more information about preprocessors
	preprocess: preprocess(),

	kit: {
		adapter: adapter({
			platform: 'node'
		}),

		// hydrate the <div id="svelte"> element in src/app.html
		target: '#svelte'
	}
};

export default config;

Could this issue and explanation be related somehow?

Possible to bypass login

Hi, I've noticed it's possible to bypass the login page and to see the page index without authenticating by adding /cfp_login to the url to access the route directly. Is there any possible fix for this? Thanks!

Fork and Deploy?

New to this - I simply forked the repo, created a pages instance, set the CFP_PASSWORD and thought it should work - what is the Pages configuration or is there something else that should be done?

Hugo site problem

Hi, thanks for making this. This is exactly what I was looking for, but I can't make it work. I copied functions folder into root of my site and uploaded zip to cloudflare. I added CFP_PASSWORD into pages environment variable. I tried encrypted an clear text passwords. I tried every posible combination i could think of but I simply can not get login page on my site. Can you give me any hint what else I can try.Thanks.

Question: Protect only some pages

Hi !
I was wondering if there is a way in the constants file to not choose the path we don't need to protect, but the page we DO need to protect.
I only have the pages with something like /protected/ in the URL.
Or maybe you have a way to do that in another way ?
Thanks !

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.