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/github
or/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
code
to request anaccess_token
. access_token
is stored in securehttpOnly
cookie.- Frontend calls
/user/profile/github
or/user/profile/google
to get user info.
🔁 The OAuth flow uses environment variables to dynamically construct the authorization and token exchange URLs.
GOOGLE_REDIRECT_URI
is used in:
- The initial redirect to Google’s OAuth consent screen
- The server-side token exchange (
/auth/google/callback
)
GITHUB_REDIRECT_URI
is recommended for consistency.
git clone https://github.com/ajmal92786/oauth-server.git
cd oauth-server
npm install
npm start
To test the backend via UI, clone the frontend:
git clone https://github.com/ajmal92786/oauth-frontend.git
cd oauth-frontend
npm install
npm run dev
Make 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/callback
VITE_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.