Skip to content

straightforward template/example for user authentication with JSON Web Tokens using FastAPI, Jinja2 templates and Traefik to enable HTTPS encryption

License

Notifications You must be signed in to change notification settings

matsjfunke/fastapi-traefik-auth

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

fastapi-login-traefik

FastAPI Python JWT

is a simple example / template for authentication

  1. auth-with-frontend uses Jinja2 templates to serve HTML to the client.
  2. 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 for building APIs with Python 3.7+.
  • 📞 Traefik as reverse proxy and load balancer, providing automatic HTTPS encryption and certificate management.
  • 🥷 Jinja2Templates for rendering dynamic HTML content, making it easy to create user interfaces.
  • 🍪 python-jose for secure user authentication using JSON Web Tokens (JWT), ensuring that only authenticated users can access protected routes.
  • 🔒 passlib for secure password hashing and verification.
  • 🗃️ SQLite Database for storing usernames and hashed passwords.
  • 🛠️ SQLAlchemy as an ORM for working with the database.
  • 🛡️ Pydantic for data validation.
  • 🐋 Docker Compose 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
git clone https://github.com/matsjfunke/fastapi-login-traefik.git
  • start container
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
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
cd auth-with-frontend
python3 -m venv env
source env/bin/activate
pip install -r tests/requirements.txt
  1. test if user credentials get saved to db
python tests/signup_test.py
  1. test authentication with the prior created username & password
python tests/login_test.py
  1. test update_username function
python tests/update_name_test.py
  1. test deletion function
python test/deletion_test.py

API example without frontend stuff

check /stand-alone-frontend

About

straightforward template/example for user authentication with JSON Web Tokens using FastAPI, Jinja2 templates and Traefik to enable HTTPS encryption

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published