FastAPI User Template
A complete, production-ready template for user authentication with FastAPI, PostgreSQL, and JWT-based cookie authentication. Designed as a starting point for projects that need a secure user system without rebuilding auth from scratch.
Code: GitHub
What it does
This template provides a fully implemented authentication system: user registration, login, logout, and token refresh using HTTP-only cookies to avoid JWT exposure in the frontend. Passwords are hashed with bcrypt, and PostgreSQL stores user records with UUID primary keys. All inputs are validated through Pydantic v2 schemas.
A comprehensive test suite covers the full auth flow — registration, login, logout, token refresh, and protected route access. The project is containerized with Docker Compose and uses Poetry for dependency management.
Tech stack
- Backend: FastAPI, Python 3.11+
- Database: PostgreSQL 13, SQLAlchemy 2.0+ ORM
- Auth: JWT tokens, bcrypt password hashing, HTTP-only cookies
- Validation: Pydantic v2
- Testing: pytest with FastAPI TestClient
- Infrastructure: Docker, Docker Compose, Poetry
Key features
- JWT authentication with HTTP-only cookie storage (no frontend token exposure)
- Registration, login, logout, and automatic token refresh endpoints
- Bcrypt password hashing with automatic salting
- UUID primary keys and Pydantic input validation
- Full auth flow test suite (pytest with coverage)
