gauth-express
v1.0.0
Published
A minimal Google OAuth2 helper for Express — no sessions required.
Maintainers
Readme
gauth-express
A minimal Google OAuth2 helper for Express — no express-sessions required. If you're looking for session support, use something like passportjs instead.
gauth-express provides two utilities:
GoogleAuth()— creates the Google OAuth login routeGoogleCallBack()— middleware that handles Google’s callback and attaches the user info toreq.GoogleID
✨ Features
- ✔ No sessions required
- ✔ Only 2 functions
- ✔ Lightweight & simple
- ✔ Returns access token, refresh token, and profile
- ✔ Adds everything to
req.GoogleID - ✔ Ideal for API servers
📦 Installation
npm install gauth-express🛠 Quick Google Setup (Required)
- Go to Google Cloud → APIs & Services → Credentials
- Create a Project/App (Type: Web Application)
- Add your Redirect URI (same one you use in your code)
- Copy your Client ID and Client Secret
⚠️ Redirect URI must match exactly
Even small differences (http vs https, localhost vs 127.0.0.1, trailing slash) will break OAuth.
🚀 Usage Example
1. Import
import { GoogleAuth, GoogleCallBack } from "gauth-express";2. Login Route
const googleLogin = GoogleAuth(
"http://localhost:3000/auth/google/callback",
"YOUR_CLIENT_ID"
);
app.get("/auth/google", googleLogin);3. Callback Route
const googleCallback = GoogleCallBack(
"YOUR_CLIENT_ID",
"YOUR_CLIENT_SECRET",
"http://localhost:3000/auth/google/callback"
);
app.get("/auth/google/callback", googleCallback, (req, res) => {
// req.GoogleID now contains:
// { Profile: ..., tokens: {...} }
res.json(req.GoogleID);
});📘 What You Get in req.GoogleID
{
Profile: ..., // Google user profile
tokens: {
access_token: "...",
refresh_token: "...",
expires_in: 3600
}
}🧩 API Reference
GoogleAuth(redirectURI, clientID)
Creates a login redirect route for Google OAuth.
Parameters:
| Name | Type | Description | |------------|--------|--------------------------------| | redirectURI | string | Your Google OAuth redirect URI | | clientID | string | Google OAuth Client ID |
GoogleCallBack(clientID, clientSecret, redirectURI)
Returns middleware that:
- Handles Google’s callback
- Exchanges the auth code for tokens
- Fetches the user’s profile
- Attaches everything to
req.GoogleID
Parameters:
| Name | Type | Description | |-------------|--------|--------------------------------| | clientID | string | Google OAuth Client ID | | clientSecret | string | Google OAuth Client Secret | | redirectURI | string | Same redirect URI used earlier |
📦 Dependencies
- axios — used for token & profile fetch
📄 License
MIT License
