npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

Iโ€™ve always been into building performant and accessible sites, but lately Iโ€™ve been taking it extremely seriously. So much so that Iโ€™ve been building a tool to help me optimize and monitor the sites that I build to make sure that Iโ€™m making an attempt to offer the best experience to those who visit them. If youโ€™re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, ๐Ÿ‘‹, Iโ€™m Ryan Hefnerย  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If youโ€™re interested in other things Iโ€™m working on, follow me on Twitter or check out the open source projects Iโ€™ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soonโ€“ish.

Open Software & Tools

This site wouldnโ€™t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you ๐Ÿ™

ยฉ 2024 โ€“ย Pkg Stats / Ryan Hefner

oauth-client-github

v1.0.5

Published

๐Ÿ” OAuth 2.0 client for GitHub

Downloads

16

Readme

oauth-client-github

node version npm version downloads count size license github-ci

๐Ÿ” OAuth 2.0 client for GitHub

Give a โญ๏ธ if this project helped you!

Preview ๐ŸŽ‰

https://oauth-client-github-demo.vercel.app/

๐Ÿ’ก Source code of this app in demo/ directory.

Installation

npm install oauth-client-github

Usage

// server.js
const oauthClientGitHub = require("oauth-client-github");

const githubAuth = oauthClientGitHub.init({
  client_id: "",
  client_secret: "",
  redirect_uri: "<host>/auth/callback",
  scope: "", // user, repo, gist, notifications, read:org, etc.
});

app.get("/auth", async (req, res) => {
  const state = req.headers.referer;
  const url = await githubAuth.buildTemporaryTokenUrl({ state });
  // Redirect to GitHub OAuth page to authorize a user
  res.redirect(url);
});

app.get("/auth/callback", async (req, res) => {
  const { code, state } = req.query;
  const response = await githubAuth.requestAccessToken({ code });
  // Create a cookie to use it on the client-side
  res.cookie("token", response.access_token);
  res.redirect(state ? String(state) : "/");
});
// client.js
const access_token = document.cookie.split("=")[1];

const response = await fetch("https://api.github.com/user", {
  headers: {
    Authorization: `Bearer ${access_token}`,
  },
});
const user = await response.json();
console.log({ user }); // A user with private data! ๐ŸŽ‰

Specification

Sequence diagram with the OAuth 2.0 flow for GitHub is defined in docs/ directory.

Prerequisites: Create new OAuth App

  1. Open a page: https://github.com/settings/developers and click on the "New OAuth App"
  2. Fill the form:
  • Application name (required)
    • eg. oauth-client-github
    • TIP: it will be visible only for you
  • Homepage URL (required)
    • eg. https://example.com
    • TIP: it will be visible only for you
  • Application description (optional)
  • Authorization callback URL (required)
    • eg. http://localhost:3000/auth/callback
    • TIP: you need to put real URL to your app
    • โš ๏ธ This field will be cross-checked with your param redirect_uri
  • Enable Device Flow (optional)
  1. Generate a new client secret by clicking on the "Generate a new client secret"
  2. Copy secret and save to config file (like .env):
    • Client ID
    • Client Secret

Parameters

  • client_id - GitHub App Client ID
  • client_secret - GitHub App Client Secret
  • redirect_uri - URL to redirect after authorization
  • scope - List of scopes separated by comma
  • state - Random string to prevent CSRF attacks (optional)
    • Or it could be a referer to redirect user to the same page after authorization
  • code - Temporary code to exchange to the access token
  • access_token - Access token to make requests to GitHub API

Development

# to rebuild dist/ & types/
npm run watch # or `npm run build` to build once

# to rebuild demo/dist/
cd demo/
npm run dev

Resources

License

The MIT License @ 2024