Code Monkey home page Code Monkey logo

cloudflare-worker-github-oauth-login's Introduction

cloudflare-worker-github-oauth-login

A Cloudflare Worker + GitHub Pages Login Example

The github-oauth-login.js file is a Cloudflare Worker which is continuously deployed using GitHub Actions (see .github/workflows/deploy.yml).

The worker does 3 things

  1. When you open the worker URL, it will redirect to the OAuth App's login URL on github.com (example).
  2. It accepts a POST request with the OAuth code retrieved from the OAuth callback redirect and returns an OAuth access token in return
  3. It enables CORS.

The index.html file is a demo of a "Login with GitHub" app, you can see the demo at gr2m.github.io/cloudflare-worker-github-oauth-login/index.html. Look at its source code. If something is unclear, please feel free to open an issue or ping me on twitter.

Step-by-step instructions to create your own

Note that you require access to the new GitHub Actions for the automated deployment to work.

  1. Create a GitHub App or GitHub OAuth App

  2. Fork this repository

  3. Create a Cloudflare account (it's free!) if you don't have one yet.

  4. Install the wrangler CLI and login with your account

    npm install --global @cloudflare/wrangler
    wrangler login
    
  5. Edit the wrangler.toml file, change the value for account_id to your own (select your account, then find your Account ID at the bottom of the side bar)

  6. Add the following secrets to your Cloudflare worker:

    • CLIENT_ID, CLIENT_SECRET: In your GitHub (OAuth) App's settings page, find Client ID and Client SECRET

      wrangler secret put CLIENT_ID
      wrangler secret put CLIENT_SECRET
      
  7. Add the following secret in your fork's repository settings:

  8. Enable GitHub Pages in your repository settings, select Source to be the master branch.

  9. In index.html, replace the gr2m workers subdomain with your own, in WORKER_URL and the login <a href="..."> tag.

That should be it. The github-oauth-login.js file will now be continously deployed to Cloudflare each time there is a commit to master.

See also

Credits

The OAuth App Avatar and this repository's social preview are using @cameronmcefee's cloud Octodex graphic :octocat:๐Ÿ’–

License

ISC

cloudflare-worker-github-oauth-login's People

Contributors

billytrend avatar gr2m avatar irvinebroque avatar xtuc avatar

Stargazers

 avatar

Watchers

 avatar

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.