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.