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

oauth2-github

v1.0.3

Published

GitHub OAuth Express Middleware This package simplifies GitHub OAuth login for your Express.js applications. It handles the entire OAuth flow, from authentication to callback, allowing you to easily retrieve user profile information.

Downloads

308

Readme

GitHub OAuth Express Middleware This package simplifies GitHub OAuth login for your Express.js applications. It handles the entire OAuth flow, from authentication to callback, allowing you to easily retrieve user profile information.

Features GitHub OAuth in a Single Route: Initiate login and handle callback all in one endpoint. Session-Based Authentication: Uses express-session and passport to handle user sessions. Flexible Configuration: Easily pass your GitHub OAuth credentials via configuration. Installation Install the package using your preferred package manager:

bash Copy code npm install oauth2-github Or, if you're using pnpm:

bash Copy code pnpm install oauth2-github Example Usage Below is an example of how to integrate this package into your Express.js application.

Step-by-Step Instructions Import the package and configure it in your app. Create the /auth/github route that handles both login and callback logic. Send user profile information back as the API response after successful authentication. js Copy code import express from "express"; import session from "express-session"; import { configureGithubOAuth, handleGithubAuth, handleGithubCallback } from "oauth2-github";

const app = express();

// Step 1: Configure GitHub OAuth with your client details const githubConfig = { clientID: "Your_GitHub_Client_ID", clientSecret: "Your_GitHub_Client_Secret", callbackURL: "http://localhost:3030/auth/github", };

// Step 2: Set up express-session app.use( session({ secret: "your_secret_key", resave: false, saveUninitialized: true, }) );

// Step 3: Configure GitHub OAuth middleware configureGithubOAuth(app, githubConfig);

// Step 4: Create a single route for both login and callback app.get("/auth/github", (req, res, next) => { if (!req.query.code) { // No GitHub code present, initiate login handleGithubAuth()(req, res, next); } else { // GitHub has redirected back with a code, handle callback handleGithubCallback()(req, res, next, () => { if (req.isAuthenticated()) { // Return the authenticated user's profile data res.json({ username: req.user.username, avatar: req.user.photos[0].value, }); } else { res.status(401).json({ error: "Unauthorized" }); } }); } });

// Step 5: Start the server app.listen(3030, () => { console.log("Server running at http://localhost:3030"); }); Configuration To use the GitHub OAuth flow, you need to pass the following configuration:

clientID: Your GitHub OAuth app's Client ID. clientSecret: Your GitHub OAuth app's Client Secret. callbackURL: The URL to which GitHub should redirect after authentication. Make sure this matches the URL you set in the GitHub Developer settings. How It Works User navigates to /auth/github: If the route is hit without a query parameter (?code=...), the user is redirected to GitHub to log in. GitHub redirects back: After successful login, GitHub redirects back to /auth/github?code=..., and the package automatically handles the callback, authenticating the user. Profile data is sent back: Upon successful authentication, the user's GitHub profile data (username and avatar) is sent back in the response. Example Flow The user hits the /auth/github route. If not already authenticated, the user is redirected to GitHub. After GitHub login, the user is redirected back to /auth/github?code=.... The app sends back the user's profile information as a JSON response.