https://github.com/mohdajmalraza/oauth-server
A lightweight Node.js OAuth2 backend for handling Google and GitHub login using Express and Axios.
https://github.com/mohdajmalraza/oauth-server
axios express-js github-login github-oauth2 gmail-login google-oauth2 node-js
Last synced: 15 days ago
JSON representation
A lightweight Node.js OAuth2 backend for handling Google and GitHub login using Express and Axios.
- Host: GitHub
- URL: https://github.com/mohdajmalraza/oauth-server
- Owner: mohdajmalraza
- Created: 2025-06-12T12:03:23.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2025-06-14T18:49:36.000Z (11 months ago)
- Last Synced: 2026-02-01T18:26:41.429Z (3 months ago)
- Topics: axios, express-js, github-login, github-oauth2, gmail-login, google-oauth2, node-js
- Language: JavaScript
- Homepage: https://oauth-server-tau.vercel.app
- Size: 23.4 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# π‘οΈ OAuth Server - GitHub & Google Login



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.
---
## π Live Demo
| Platform | Frontend | Backend |
| --------- | --------------------------------------------------- | ------------------------------------------------ |
| π GitHub | [Frontend](https://oauth-frontend-eac27.vercel.app) | [Backend](https://oauth-server-eac27.vercel.app) |
---
## π Table of Contents
- [Features](#-features)
- [Technologies Used](#-technologies-used)
- [How OAuth Flow Works](#-how-oauth-flow-works)
- [Run Locally](#-run-locally)
- [Environment Variables](#-environment-variables)
- [Test the App](#-test-the-app)
- [Folder Structure](#-folder-structure)
- [License](#-license)
---
## β¨ Features
- π OAuth 2.0 login with GitHub & Google
- πͺ Secure cookie handling with `httpOnly`, `secure`, and `SameSite`
- π§ Token verification middleware
- π₯ Clean API endpoints to fetch user profiles
- π Deployed on Vercel
---
## π Technologies Used
- **Node.js** & **Express**
- **Axios** for HTTP requests
- **cookie-parser** for cookie handling
- **dotenv** for environment config
- **Vercel** for deployment
---
## π How OAuth Flow Works
1. User clicks "Login with GitHub" or "Login with Google" on frontend.
2. Frontend redirects user to backend (`/auth/github` or `/auth/google`).
3. Backend redirects user to the GitHub or Google OAuth consent screen.
4. After consent, the provider redirects back to backend with a `code`.
5. Backend uses that `code` to request an `access_token`.
6. `access_token` is stored in secure `httpOnly` cookie.
7. 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.
---
## π§βπ» Run Locally
### Clone the repo:
```bash
git clone https://github.com/ajmal92786/oauth-server.git
cd oauth-server
npm install
npm start
```
### Related Frontend Repo
To test the backend via UI, clone the frontend:
```bash
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`
---
## π§Ύ Environment Variables
### π `.env` for `oauth-server`:
```env
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
```
### π `.env` for `oauth-frontend`:
```env
VITE_SERVER_BASE_URL=http://localhost:4000
VITE_GITHUB_API_BASE_URL=https://api.github.com
```
π Also provide a `.env.example` for contributors.
---
## π§ͺ Test the App
- Visit the frontend: [oauth-frontend.vercel.app](https://oauth-frontend-eac27.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
---
## π Folder Structure
```
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
```
---
## π License
This project is for educational purposes. Feel free to fork and use for your own learning or demo needs.
---