A minimal and secure OAuth 2.0 server built with Node.js, Express, and Axios to handle GitHub and Google login flows, cookie-based token management, and profile retrieval.
| Platform | Frontend | Backend |
|---|---|---|
| π GitHub | Frontend | Backend |
- Features
- Technologies Used
- How OAuth Flow Works
- Run Locally
- Environment Variables
- Test the App
- Folder Structure
- License
- π OAuth 2.0 login with GitHub & Google
- πͺ Secure cookie handling with
httpOnly,secure, andSameSite - π§ Token verification middleware
- π₯ Clean API endpoints to fetch user profiles
- π Deployed on Vercel
- Node.js & Express
- Axios for HTTP requests
- cookie-parser for cookie handling
- dotenv for environment config
- Vercel for deployment
- User clicks "Login with GitHub" or "Login with Google" on frontend.
- Frontend redirects user to backend (
/auth/githubor/auth/google). - Backend redirects user to the GitHub or Google OAuth consent screen.
- After consent, the provider redirects back to backend with a
code. - Backend uses that
codeto request anaccess_token. access_tokenis stored in securehttpOnlycookie.- Frontend calls
/user/profile/githubor/user/profile/googleto get user info.
π The OAuth flow uses environment variables to dynamically construct the authorization and token exchange URLs.
GOOGLE_REDIRECT_URIis used in:
- The initial redirect to Googleβs OAuth consent screen
- The server-side token exchange (
/auth/google/callback)
GITHUB_REDIRECT_URIis recommended for consistency.
git clone https://github.com/ajmal92786/oauth-server.git
cd oauth-server
npm install
npm startTo test the backend via UI, clone the frontend:
git clone https://github.com/ajmal92786/oauth-frontend.git
cd oauth-frontend
npm install
npm run devMake sure:
- Backend is running on:
http://localhost:4000 - Frontend is running on:
http://localhost:3000
PORT=4000
FRONTEND_URL=http://localhost:3000
# GitHub OAuth
GITHUB_CLIENT_ID=your_github_client_id
GITHUB_CLIENT_SECRET=your_github_client_secret
# Google OAuth
GOOGLE_CLIENT_ID=your_google_client_id
GOOGLE_CLIENT_SECRET=your_google_client_secret
# Redirect URIs
GOOGLE_REDIRECT_URI=http://localhost:4000/auth/google/callbackVITE_SERVER_BASE_URL=http://localhost:4000
VITE_GITHUB_API_BASE_URL=https://api.github.comπ Also provide a .env.example for contributors.
- Visit the frontend: oauth-frontend.vercel.app
- Click βLogin with GitHubβ or βLogin with Googleβ
- Authorize the app
- Your profile info will be fetched via backend and displayed on the frontend
oauth-server/
βββ middleware/
β βββ index.js # Access token verification
βββ services/
β βββ index.js # Cookie utility functions
βββ .env.example
βββ .gitignore
βββ index.js # Main Express server
βββ package.json
βββ vercel.json
βββ README.md
This project is for educational purposes. Feel free to fork and use for your own learning or demo needs.