A production-ready authentication and session management system built with FastAPI. It provides robust, secure primitives for JWT-based auth, sessions, email verification, password reset, and RBAC, with clean architecture and deploy-ready tooling.
- JWT authentication with access and refresh tokens (HttpOnly cookies)
- Device-aware sessions (IP and User-Agent fingerprint)
- Argon2 password hashing (memory-hard)
- Email verification and password reset flows
- Role-based access control (RBAC)
- Rate limiting for login attempts (Redis or in-memory fallback)
- SQLAlchemy 2.0 + Alembic migrations
- Dockerfiles and compose for local and production
app/
api/ # routers and dependencies
core/ # config, security, rate limiting
db/ # engine/session setup
models/ # SQLAlchemy models
schemas/ # Pydantic models
services/ # business logic (auth, sessions, email)
utils/ # helpers (cookies, etc.)
main.py # FastAPI app factory/assembly
alembic/ # database migrations
- Copy environment template and configure values:
cp env.example .env- Start services (development):
docker compose up --build- Apply migrations:
docker compose exec api alembic upgrade head- Open API docs:
http://localhost:8000/docs
- Create a virtual environment and install dependencies:
pip install -r requirements.txt-
Provision a database (PostgreSQL recommended) and set env vars. For SQLite, set
DB_BACKEND=sqliteand optionallySQLITE_PATH. -
(Optional) Start Redis for rate limiting, or leave
REDIS_URLunset to use in-memory fallback (seeapp/core/rate_limit.py). -
Run the server:
uvicorn app.main:app --reload --port 8000Configuration is managed by Pydantic Settings from .env (see app/core/config.py). A ready-to-copy template is provided in env.example.
Key variables:
SECRET_KEY(required)DATABASE_URL(for Postgres) orDB_BACKEND=sqlitewithSQLITE_PATHCOOKIE_SECUREandCOOKIE_SAMESITEfor cookie policyREDIS_URL(optional). If not set, rate limiting falls back to in-memory per-process storage
Generate and apply migrations with Alembic:
alembic revision --autogenerate -m "message"
alembic upgrade headpytest -qUse the production compose file (Gunicorn + Uvicorn workers):
cp env.example .env
docker compose -f docker-compose.prod.yml up --build -d
docker compose -f docker-compose.prod.yml exec app alembic upgrade headRecommendations:
- Provide a strong
SECRET_KEYand a productionDATABASE_URL - Run behind a TLS-terminating proxy and set
COOKIE_SECURE=true - Set
REDIS_URLto enable distributed rate limiting (optional)
- Passwords are hashed with Argon2
- JWTs are set in HttpOnly cookies to mitigate XSS token theft
- Refresh tokens rotate; previous tokens are invalidated
- Login attempts are rate limited via Redis when available
Licensed under the MIT License. See LICENSE for details.