An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

# fastapi-login-traefik
![FastAPI](https://img.shields.io/badge/FastAPI-005571?style=for-the-badge&logo=fastapi)
![Python](https://img.shields.io/badge/python-3670A0?style=for-the-badge&logo=python&logoColor=ffdd54)
![JWT](https://img.shields.io/badge/JWT-black?style=for-the-badge&logo=JSON%20web%20tokens)

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