Giter Site home page Giter Site logo

max-elia / poc-kv-auth Goto Github PK

View Code? Open in Web Editor NEW
6.0 1.0 1.0 28 KB

Session-based user authentication with Cloudflare KV

License: MIT License

JavaScript 39.54% HTML 52.39% CSS 8.07%
authentication key-value cloudflare cloudflare-pages cloudflare-pages-functions

poc-kv-auth's Introduction

Cloudflare KV-Auth

This is an example of how one could use Cloudflare's distributed and fast KV data store together with Cloudflare Pages as a user authentication method.

This is a Proof of concept so DO NOT USE IT FOR PRODUCTION.

Since data is replicated around the world in KV, this method is also not EU GDPR compliant.

๐Ÿš€ Demo

Try now

๐Ÿ“ƒ Documentation

Generally it is not recommended to store user's passwords and you should avoid it as much as possible, by instead using OAuth Providers or using E-Mail Sign in with magic-links.

Pages comes with a feature called Pages functions where you can execute server-side code on the edge. The KV data store is directly available to those Pages functions. This projects consists of two root folders:

/public (Frontend)

This folder contains the static html ressources. The forms submit the user data to functions endpoints.

/functions (Backend)

This folder contains the API endpoints for registering a user, logging in and out. Cloudflare Pages uses file-based routing and creates the endpoints at the specified route. (starting from functions as root). The /function/protected/_middleware.js function checks for a valid session_id and then forwards the request to the static assets handler if authenticated.

๐Ÿ‘ฉโ€๐Ÿ’ป Setup in Cloudflare

  1. Fork repository (or create own)
  2. Create New Pages project in Cloudflare dashboard and connect to repo
  3. Set Build output directory: to 'public'. Leave Build command blank.
  4. Generate secure token
openssl rand -base64 32
  1. Create environment variable 'SECRET' in dashboard and paste secure token. You should also hit the encrypt button so not even you but only your functions have access to the secret
  2. Trigger new Deployment with a commit to the repository or from the dashboard.

๐Ÿ› ๏ธ Development (local)

Step 0: Clone the repo:

git clone https://github.com/max-elia/poc-kv-auth.git

Step 1: Install wrangler Instructions here

Step 2: Install dependencies

npm install // (or pnpm)

Step 3: Create secure token and paste in SECRET variable in .dev.vars file

openssl rand -base64 32

Step 4: Run locally

npm run dev

Feedback

If you have any feedback, feel free to open an issue or pull request.

โœ๏ธ Authors

๐Ÿ’ผ License

MIT

poc-kv-auth's People

Contributors

max-elia avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

depositdraft

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.