An open API service indexing awesome lists of open source software.

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.

Awesome Lists containing this project

README

          

# πŸ›‘οΈ OAuth Server - GitHub & Google Login

![Node.js](https://img.shields.io/badge/Node.js-18.x-green?logo=node.js)
![Express](https://img.shields.io/badge/Express.js-Backend-lightgrey?logo=express)
![Vercel](https://img.shields.io/badge/Deployed%20on-Vercel-black?logo=vercel)

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.

---