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

https://github.com/persteenolsen/fastapi-auth-demo

Python FastAPI serving Authentication by JWT towards a PostgreSQL Database
https://github.com/persteenolsen/fastapi-auth-demo

alembic fastapi postgresql python

Last synced: 3 months ago
JSON representation

Python FastAPI serving Authentication by JWT towards a PostgreSQL Database

Awesome Lists containing this project

README

          

# Python + FastAPI + PostgreSQL + Alembic + SQLAlchemy and JWT Authentication

A REST API that serves Registration + JWT Authentication

Last updated:

- 28-03-2026

Python Version:

- 3.12

# Get startet

- Clone the repository from my GitHub

- Create a virtual environment by Powershell or VS Code:

"python -m venv "

- Go to the virtual environment's directory and activate it:

"Scripts/activate"

- Install the requirements:

"pip3 install -r requirements.txt"

# Try the Vue 3 frontend for testing the Web API

- [`The Vue 3 Client at GitHub`](https://github.com/persteenolsen/vue-fastapi-jwt-auth-client) - The Vue 3 Client using JWT Authentication

# Swagger documentation / Testing the API

FastAPI provides the Swagger documentation of the API where you can perform CRUD operations

To access the documentation, we must run uvicorn:

"uvicorn main:app --reload"

If everything works fine, the FastAPI and Swagger documentation is now available at:

`http://127.0.0.1:8000/docs`

- Use the Swagger for Register a User in the PostgreSQL DB and try to perform Login operations

- You can go to the PostgreSQL at Neon to test your data in the DB

When you make a change to the models and start run the Web App the PostgreSQL should be updated

# JWT Authentication

- The token will expire after 5 minutes for testing and demo. Then a 401 status will happen

# The structure of the API by folders for scalability

- The Database functionality is placed in files inside the directory db

- The User Routes are placed in files inside the directory routes/user.py

- The simple Routes are placed in files inside the directory routes/simple.py

- The Models are placed in files inside the directory models

- The Schemas are placed in files inside the directory schemas

- The functionality of authentication is placed inside the directory security

- The functionality like get current user, called from the Routes, is placed inside the directory services

# Migration with Alembic

- Install and use Alembic for Migration and run:

- alembic init alembic

- For demonstration I added name in the User Model and Schema and generated and applied migration:

- alembic revision --autogenerate -m "create column name"

- alembic upgrade head

- Then run the FastAPI again and check that everything works fine

# Deployment to Vercel

- Take a look at the file "vercel.json"

- Create a Project at Vercel from your repository at GitHub with the code of this FastAPI

- Create the envirement variables from .env at Vercel with the connection to PostgreSQL

- Make a commit to your GitHub

- Go to Vercel and check that the build and deployment happened and your site is in Production

Happy use of FastAPI :-)