@mantlejs/auth-github
GitHub OAuth 2.0 strategy for Mantle JS. Implements the authorization code flow — no Passport.js dependency. Registers GET /auth/github and GET /auth/github/callback on the configured HTTP transport (@mantlejs/express, @mantlejs/koa, or @mantlejs/http), then finds or creates a user record and returns a Mantle JWT pair.
Installation
Section titled “Installation”npm install @mantlejs/auth-githubConcepts
Section titled “Concepts”Authorization code flow
Section titled “Authorization code flow”GitHub Sign-In uses the standard authorization code flow (without PKCE — GitHub does not support it for server-side apps). On each redirect request the plugin generates a random state token stored server-side for CSRF protection and passed through the OAuth exchange. URL construction and token exchange go through Arctic (see ADR-002); the token request authenticates with HTTP Basic credentials.
Find-or-create
Section titled “Find-or-create”On callback the plugin searches the configured user service for a record where githubId (configurable) matches the id field from GitHub’s /user endpoint. If no record is found, it creates one with { githubId, email, name } from the profile. The same user is returned on every subsequent sign-in.
Quick start
Section titled “Quick start”import { mantle } from "@mantlejs/mantle";import { express } from "@mantlejs/express";import { auth } from "@mantlejs/auth";import { githubStrategy } from "@mantlejs/auth-github";
const app = mantle() .configure(express()) .configure(auth({ secret: process.env.JWT_SECRET! })) .configure( githubStrategy({ clientId: process.env.GITHUB_CLIENT_ID!, clientSecret: process.env.GITHUB_CLIENT_SECRET!, }), );
app.listen(3030);Sign-in flow:
- Redirect the browser to
GET /auth/github - User authenticates on GitHub and consents
- GitHub redirects to
GET /auth/github/callback?code=...&state=... - The plugin exchanges the code, fetches the profile, finds or creates the user, and responds:
{ "accessToken": "eyJhbGciOiJIUzI1NiJ9...", "refreshToken": "eyJhbGciOiJIUzI1NiJ9...", "user": { "id": "1", "githubId": "12345", "email": "alice@github.com", "name": "Alice" }}githubStrategy(config)
Section titled “githubStrategy(config)”function githubStrategy(config: GitHubStrategyConfig): MantlePlugin;
type GitHubStrategyConfig = { clientId: string; clientSecret: string; callbackUrl?: string; // Default: '/auth/github/callback' scope?: string[]; // Default: ['read:user', 'user:email'] entity?: string; // Default: 'users' entityIdField?: string; // Default: 'githubId'};| Field | Type | Default | Description |
|---|---|---|---|
clientId |
string |
— | GitHub OAuth App client ID (required) |
clientSecret |
string |
— | GitHub OAuth App client secret (required) |
callbackUrl |
string |
'/auth/github/callback' |
Callback path — must match the Authorization callback URL registered in your GitHub OAuth App |
scope |
string[] |
['read:user', 'user:email'] |
GitHub OAuth scopes |
entity |
string |
'users' |
Mantle service used to find or create users |
entityIdField |
string |
'githubId' |
Field on the user record matched against GitHub’s id value |
redirectUrl |
string |
none — returns JSON | Frontend URL to redirect to on completion, with tokens (or an error) in the URL fragment — see @mantlejs/auth-oauth |
Routes registered:
| Method | Path | Description |
|---|---|---|
GET |
/auth/github |
Redirect to GitHub consent screen |
GET |
/auth/github/callback |
Handle callback, issue Mantle JWT pair |
Must be configured after an HTTP transport (@mantlejs/express, @mantlejs/koa, or @mantlejs/http) and auth().
import type { GitHubStrategyConfig } from "@mantlejs/auth-github";GitHubStrategyConfig is an alias for OAuthPluginConfig from @mantlejs/auth-oauth.
GitHub OAuth App setup
Section titled “GitHub OAuth App setup”- Go to Settings → Developer settings → OAuth Apps at github.com
- Click New OAuth App
- Set the Authorization callback URL to
https://your-domain.com/auth/github/callback - Copy the Client ID and generate a Client Secret
- Add both to your environment variables
Development
Section titled “Development”npx nx build auth-github # compilenpx nx test auth-github # run testsnpx nx lint auth-github # lintPublishing
Section titled “Publishing”npx nx build auth-githubcd packages/auth-githubnpm publish --access public