Production-ready FastAPI backend implementing a secure Role-Based Access Control (RBAC) authentication system.
This API supports JWT-based authentication, email OTP verification, role-based authorization, and is deployed on AWS EC2 using Gunicorn and Nginx with HTTPS.
The project demonstrates:
-
Secure authentication flows
-
Separation of concerns (modular architecture)
-
Environment-based configuration
-
Production deployment setup
-
User Registration with Email OTP verification
-
Secure Login with JWT (Access Token)
-
Password Hashing using Passlib (bcrypt)
-
Token validation & protected routes
-
Role-based access (Admin / User)
-
Route-level permission enforcement
-
Secure dependency-based authorization
-
Password Reset Flow
-
Password Change
-
Profile Management
-
Admin User Management
-
Admin Job Management
-
Role assignment control
-
Password hashing with bcrypt
-
JWT expiration handling
-
Environment-based secrets management
-
Route-level access control
-
Separation of auth logic and business logic
-
FastAPI – High-performance API framework
-
SQLAlchemy – ORM for database interaction
-
MySQL – Relational database
-
python-jose – JWT handling
-
Passlib (bcrypt) – Secure password hashing
-
Gunicorn – Production ASGI server
-
Nginx – Reverse proxy
-
AWS EC2 – Cloud deployment
fastapi-rbac-auth-api/
├── app/
│ ├── core/ # Security, JWT, config
│ ├── models/ # SQLAlchemy models
│ ├── routes/ # API endpoints
│ ├── schemas/ # Pydantic request/response models
│ ├── utils/ # Utility functions
│ ├── database.py # DB connection setup
│ └── main.py # FastAPI app entry point
├── requirements.txt
├── .env.example
└── .gitignore
The application uses environment variables for secure configuration.
- Create a .env file based on .env.example.
Example:
# Database Configuration
DB_USER=your_database_username
DB_PASSWORD=your_password
DB_HOST=localhost
DB_DATABASE_NAME=your_database_name
# JWT Configuration
SECRET_KEY=your_secret_key_here
ACCESS_TOKEN_EXPIRE_MINUTES=30
# Email (OTP)
EMAIL_ADDRESS=your_email@gmail.com
EMAIL_PASSWORD=your_email_password
Sensitive credentials are never stored in the repository.
python -m venv venv
- Activate it:
->Windows:
venv\Scripts\activate
->Linux / macOS:
source venv/bin/activate
pip install -r requirements.txt
cp .env.example .env
Update the variables accordingly.
uvicorn app.main:app --reload
http://127.0.0.1:8000
http://127.0.0.1:8000/docs
This backend is deployed on AWS EC2 with the following setup:
-
Gunicorn (ASGI server)
-
Nginx (reverse proxy)
-
HTTPS (SSL certificate)
-
systemd service for process management
-
Production start command:
-
gunicorn app.main:app -k uvicorn.workers.UvicornWorker
Nginx routes external HTTPS traffic to the internal Gunicorn server running on port 8000.