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

github-auth-library

v1.0.5

Published

A Node.js SDK for GitHub OAuth and User APIs

Downloads

9

Readme

GitHub Auth Library

The github-auth-library is a Node.js library designed to handle OAuth authentication with GitHub. It provides a simple interface to authenticate users, retrieve access tokens, and fetch user details using GitHub's API. This library is designed to follow the SOLID principles, making it extensible and maintainable.

Features

  • Easy-to-use methods for GitHub OAuth authentication.
  • Methods to exchange authorization code for access token.
  • Methods to fetch user details using the access token.
  • Structured to follow SOLID principles for better maintainability and extensibility.
  • Ready for integration into your Node.js application.

Installation

npm install github-auth-library

Usage

Initialize the GitHub SDK

To use the library, you need to initialize the GitHubSDK with your GitHub OAuth application's client ID and client secret.

const GitHubSDK = require("github-auth-library");

const githubSDK = new GitHubSDK(
  "YOUR_GITHUB_CLIENT_ID",
  "YOUR_GITHUB_CLIENT_SECRET"
);

Exchange Authorization Code for Access Token

You can exchange the authorization code received from GitHub for an access token using the getToken method.

const code = "AUTHORIZATION_CODE_RECEIVED_FROM_GITHUB";
const redirectUri = "YOUR_REDIRECT_URI";

githubSDK
  .getToken(code, redirectUri)
  .then((token) => {
    console.log("Access Token:", token);
  })
  .catch((error) => {
    console.error("Error getting access token:", error);
  });

Fetch User Details

Once you have the access token, you can fetch the user details using the getUserInfo method.

const token = {
  access_token: "YOUR_ACCESS_TOKEN",
};

githubSDK
  .getUserInfo(token)
  .then((userInfo) => {
    console.log("User Info:", userInfo);
  })
  .catch((error) => {
    console.error("Error getting user info:", error);
  });

Methods

getToken(code, redirectUri)

  • Description: Exchanges the authorization code for an access token.
  • Parameters:
    • code (string): The authorization code received from GitHub.
    • redirectUri (string): The redirect URI registered with your GitHub OAuth application.
  • Returns: A promise that resolves to the access token.
  • Reference: GitHub OAuth Documentation

getUserInfo(token)

  • Description: Fetches user details using the access token.
  • Parameters:
    • token (object): An object containing the access token (access_token).
  • Returns: A promise that resolves to the user details.
  • Reference: GitHub Users API Documentation

Example

Here is an example of using the github-auth-library in an Express.js application to handle GitHub OAuth authentication.

const express = require("express");
const GitHubSDK = require("github-auth-library");
const app = express();

const githubSDK = new GitHubSDK(
  "YOUR_GITHUB_CLIENT_ID",
  "YOUR_GITHUB_CLIENT_SECRET"
);

app.post("/github-auth", async (req, res) => {
  const { code, redirectUri } = req.body;

  try {
    const token = await githubSDK.getToken(code, redirectUri);
    const userInfo = await githubSDK.getUserInfo(token);
    res.status(200).json({ user: userInfo, token });
  } catch (error) {
    res.status(401).json({ message: "Unauthorized", error });
  }
});

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}`);
});

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Resources