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

https://github.com/persteenolsen/fastapi-jwt-auth-mongo-render-two

Python and FastAPI with JWT Auth hosted at Render using MongoDB at Atlas
https://github.com/persteenolsen/fastapi-jwt-auth-mongo-render-two

fastapi jwt mongodb python render

Last synced: 9 days ago
JSON representation

Python and FastAPI with JWT Auth hosted at Render using MongoDB at Atlas

Awesome Lists containing this project

README

          

# ๐Ÿš€ FastAPI Auth System (JWT + MongoDB + bcrypt)

Last updated:

- 03-06-2026

# Version

At Render I use the PYTHON_VERSION environment variable to tell Render to use Python version 3.11. Locally I am using Python 3.12

A production-ready authentication backend built with **FastAPI**, **MongoDB Atlas**, **JWT authentication**, and **bcrypt password hashing**, deployed on **Render**.

---

## ๐Ÿ“Œ Overview

This project provides a secure backend API with:

- ๐Ÿ” User authentication (JWT-based)
- ๐Ÿง‚ Secure password hashing (bcrypt)
- ๐Ÿ—„๏ธ MongoDB Atlas integration (async via Motor)
- โ˜๏ธ Render deployment ready
- ๐Ÿงช Development mode toggle (`DEV_MODE`)
- โšก High-performance async API using FastAPI

---

## โš™๏ธ Tech Stack

- **Backend:** FastAPI
- **Database:** MongoDB Atlas
- **Driver:** Motor (async MongoDB driver)
- **Auth:** JWT (python-jose)
- **Security:** bcrypt password hashing
- **Server:** Uvicorn / Render

---

## ๐Ÿ“‚ Features

### ๐Ÿ” Authentication
- Register users (dev-only endpoint)
- Login with username & password
- JWT token generation (30 min expiry)
- Protected routes with Bearer token

### ๐Ÿ—„๏ธ Database
- MongoDB Atlas integration
- Async database operations (Motor)
- Secure user storage with hashed passwords

### ๐Ÿ›ก๏ธ Security
- bcrypt hashed passwords (no plaintext storage)
- JWT-based authentication
- Environment-based configuration
- DEV_MODE protection for sensitive endpoints

---

## ๐Ÿ“ Project Structure

app.py

requirements.txt

.env

### ๐Ÿ”‘ Environment Variables

Create a .env file:

ATLAS_URI=mongodb+srv://:@cluster.mongodb.net/
DB_NAME=your_db_name
COLLECTION_NAME=users

SECRET_KEY=your_secret_key
ALGORITHM=HS256

DEV_MODE=true

โš ๏ธ Set DEV_MODE=false on Render for production.

## ๐Ÿš€ Installation

### 1. Clone the repository

git clone https://github.com/your-username/fastapi-auth-mongo.git
cd fastapi-auth-mongo

---

### 2. Create virtual environment

python -m venv venv
source venv/bin/activate # macOS/Linux
venv\Scripts\activate # Windows

---

### 3. Install dependencies

pip install -r requirements.txt

---

### 4. Run locally

uvicorn app:app --reload

---

API will be available at:

http://127.0.0.1:8000

Swagger docs:

http://127.0.0.1:8000/docs

---

## ๐Ÿ” API Endpoints

### ๐ŸŸข Health Check

GET /health

Response:
{
"status": "ok"
}

---

### ๐Ÿง‘ Create User (DEV ONLY)

POST /create-user

Request body:
{
"username": "testuser",
"password": "admin123"
}

Response:
{
"message": "User created (DEV MODE)",
"user_id": "mongodb_object_id"
}

๐Ÿšจ Disabled automatically when DEV_MODE=false

---

### ๐Ÿ”‘ Login (Get Token)

POST /token

Form data:
username=testuser
password=admin123

Response:
{
"access_token": "JWT_TOKEN_HERE",
"token_type": "bearer"
}

---

### ๐Ÿ”’ Protected Route

GET /protected

Headers:
Authorization: Bearer JWT_TOKEN_HERE

Response:
{
"message": "Hello testuser"
}

---

## ๐Ÿงช Development Mode

DEV_MODE controls whether user creation is allowed.

Mode | Behavior
-----|---------
true | /create-user enabled
false | /create-user blocked (403)

---

## โ˜๏ธ Deployment (Render)

### Build Command

pip install -r requirements.txt

### Start Command

uvicorn app:app --host 0.0.0.0 --port 10000

### Environment Variables (Render Dashboard)

ATLAS_URI

DB_NAME

COLLECTION_NAME

SECRET_KEY

ALGORITHM

DEV_MODE=false

---

## ๐Ÿ” Security Notes

- Passwords are hashed using bcrypt (salted)
- JWT tokens expire after 30 minutes
- No plaintext passwords stored
- DEV endpoints disabled in production

---

## ๐Ÿ“ˆ Future Improvements

- ๐Ÿ” Refresh tokens
- ๐Ÿšช Logout / token revocation
- โš–๏ธ Role-based access control (RBAC)
- ๐Ÿšฆ Rate limiting login attempts
- ๐Ÿ“ง Email verification
- ๐Ÿง  Password strength validation

---

## ๐Ÿ‘จโ€๐Ÿ’ป Author

Built with FastAPI, MongoDB, and bcrypt for secure backend authentication systems.

---

## โญ License

MIT License