https://github.com/matsjfunke/fastapi-traefik-auth
straightforward template/example for user authentication with JSON Web Tokens using FastAPI, Jinja2 templates and Traefik to enable HTTPS encryption
https://github.com/matsjfunke/fastapi-traefik-auth
authentication cookies docker-compose fastapi jinja2-templates jose-jwt passlib pydantic sqlite traefik
Last synced: about 2 months ago
JSON representation
straightforward template/example for user authentication with JSON Web Tokens using FastAPI, Jinja2 templates and Traefik to enable HTTPS encryption
- Host: GitHub
- URL: https://github.com/matsjfunke/fastapi-traefik-auth
- Owner: matsjfunke
- License: apache-2.0
- Created: 2024-04-14T13:46:56.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-12-23T20:55:56.000Z (over 1 year ago)
- Last Synced: 2025-02-02T09:41:28.328Z (over 1 year ago)
- Topics: authentication, cookies, docker-compose, fastapi, jinja2-templates, jose-jwt, passlib, pydantic, sqlite, traefik
- Language: Python
- Homepage:
- Size: 52.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# fastapi-login-traefik



is a simple example / template for authentication
1. [auth-with-frontend](auth-with-frontend) uses Jinja2 templates to serve HTML to the client.
2. [stand-alone-backend](stand-alone-backend) uses CORS to allow cross-origin requests, enabling the backend to communicate with frontend applications hosted on different domains.
## Technology Stack and Features
- โก [FastAPI](https://fastapi.tiangolo.com) for building APIs with Python 3.7+.
- ๐ [Traefik](https://traefik.io) as reverse proxy and load balancer, providing automatic HTTPS encryption and certificate management.
- ๐ฅท [Jinja2Templates](https://fastapi.tiangolo.com/advanced/templates/) for rendering dynamic HTML content, making it easy to create user interfaces.
- ๐ช [python-jose](https://python-jose.readthedocs.io/en/latest/) for secure user authentication using JSON Web Tokens (JWT), ensuring that only authenticated users can access protected routes.
- ๐ [passlib](https://pypi.org/project/passlib/) for secure password hashing and verification.
- ๐๏ธ [SQLite Database](https://www.sqlite.org/) for storing usernames and hashed passwords.
- ๐ ๏ธ [SQLAlchemy](https://www.sqlalchemy.org/) as an ORM for working with the database.
- ๐ก๏ธ [Pydantic](https://docs.pydantic.dev) for data validation.
- ๐ [Docker Compose](https://www.docker.com) to deploy and manage your application, allowing for containerization and orchestration of your services.
## Quick start / Usage
#### 1. run local without reverse-proxy
- cloe repo
```bash
git clone https://github.com/matsjfunke/fastapi-login-traefik.git
```
- start container
```bash
cd auth-with-frontend
docker-compose -f docker-compose.yml up --build
```
- than access the localhost:8000 and submit username and password, then enter your credentials at localhost:8000/login
- now with the cookies you obtained through logging in you can access the /hello and /users endpoints
- at /hello you can delete your username, password & cookies or update your username
#### 2. run on server
- clone repo
- change line 34 of docker.compose.staging.yml
- "traefik.http.routers.db-access.rule=Host(`your-domain.com`)" # change `your-domain.com` to your domain
- start docker
```bash
git clone https://github.com/matsjfunke/fastapi-login-traefik.git
cd auth-with-frontend
docker-compose -f docker-compose.staging.yml up
```
- than access the `your-domain.com` and submit username and password, then enter your credentials at `your-domain.com/login`
- now with the cookies you obtained through logging in you can access the /hello and /users endpoints
- at /hello you can delete your username, password & cookies or update your username
## Test the Code
test all CRUD functions in this order
1. install dependencyโs
```bash
cd auth-with-frontend
python3 -m venv env
source env/bin/activate
pip install -r tests/requirements.txt
```
2. test if user credentials get saved to db
```bash
python tests/signup_test.py
```
3. test authentication with the prior created username & password
```bash
python tests/login_test.py
```
4. test update_username function
```bash
python tests/update_name_test.py
```
5. test deletion function
```bash
python test/deletion_test.py
```
## API example without frontend stuff
check /stand-alone-frontend