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

@jimmycode/simple-oauth2-github

v0.6.1

Published

A simple Node.js client library for GitHub OAuth2.

Downloads

6

Readme

Simple OAuth2 Github

This library is a wrapper around Simple OAuth2 Library

Specially made for Authorization Code Flow with GitHub.

Requirements

Latest Node 8 LTS or newer versions.

Getting started

npm install --save simple-oauth2 @jimmycode/simple-oauth2-github

or

yarn add simple-oauth2 @jimmycode/simple-oauth2-github

Usage

const simpleOAuth2Github = require('@jimmycode/simple-oauth2-github');
const github = simpleOAuth2Github.create(options);

github object exposes 3 keys:

  • authorize: Middleware to request user's authorization.
  • getToken: Middleware for callback processing and exchange the authorization token for an access_token
  • oauth2: The underlying simple-oauth2 instance.

Options

SimpleOAuth2Github comes with default values for most of the options.

Required options

| Option | Description | |--------------|-----------------------------------------------| | clientId | Your App Id. | | clientSecret | Your App Secret Id. | | callbackURL | Callback configured when you created the app. |

Other options

| Option | Default | Description | |------------------|------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | scope | ['read:user', 'read:email'] | https://developer.github.com/apps/building-oauth-apps/understanding-scopes-for-oauth-apps | | state | '' | Your CSRF anti-forgery token. More at: https://auth0.com/docs/protocols/oauth2/oauth-state | | returnError | false | When is false (default), will call the next middleware with the error object. When is true, will set req.tokenError to the error, and call the next middleware as if there were no error. | | authorizeHost | 'https://github.com' | | | authorizePath | '/login/oauth/authorize' | | | tokenHost | 'https://github.com' | | | tokenPath | '/login/oauth/access_token' | | | authorizeOptions | {} | Pass extra parameters when requesting authorization. | | tokenOptions | {} | Pass extra parameters when requesting access_token. |

Example

Original boilerplate

const oauth2 = require('simple-oauth2').create({
  client: {
    id: process.env.GITHUB_CLIENT_ID,
    secret: process.env.GITHUB_CLIENT_SECRET
  },
  auth: {
    authorizeHost: 'https://github.com',
    authorizePath: '/login/oauth/authorize',

    tokenHost: 'https://github.com',
    tokenPath: '/login/oauth/access_token'
  }
});

router.get('/auth/github', (req, res) => {
  const authorizationUri = oauth2.authorizationCode.authorizeURL({
    redirect_uri: 'http://localhost:3000/auth/github/callback',
    scope: ['read:user', 'read:email']
  });

  res.redirect(authorizationUri);
});

router.get('/auth/github/callback', async(req, res) => {
  const code = req.query.code;
  const options = {
    code,
    redirect_uri: 'http://localhost:3000/auth/github/callback'
  };

  try {
    // The resulting token.
    const result = await oauth2.authorizationCode.getToken(options);

    // Exchange for the access token.
    const token = oauth2.accessToken.create(result);

    return res.status(200).json(token);
  } catch (error) {
    console.error('Access Token Error', error.message);
    return res.status(500).json('Authentication failed');
  }
});

With SimpleOAuth2Github

const simpleOAuth2Github = require('@jimmycode/simple-oauth2-github');

const github = simpleOAuth2Github.create({
  clientId: process.env.GITHUB_CLIENT_ID,
  clientSecret: process.env.GITHUB_CLIENT_SECRET,
  callbackURL: 'http://localhost:3000/auth/github/callback'
});

// Ask the user to authorize.
router.get('/auth/github', github.authorize);

// Exchange the token for the access token.
router.get('/auth/github/callback', github.accessToken, (req, res) => {
  return res.status(200).json(req.token);
});