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
- Host: GitHub
- URL: https://github.com/persteenolsen/fastapi-auth-demo
- Owner: persteenolsen
- Created: 2025-12-19T07:42:48.000Z (6 months ago)
- Default Branch: main
- Last Pushed: 2026-02-13T09:04:35.000Z (5 months ago)
- Last Synced: 2026-02-13T17:13:39.010Z (5 months ago)
- Topics: alembic, fastapi, postgresql, python
- Language: Python
- Homepage: https://fastapi-auth-demo.vercel.app/docs
- Size: 81.1 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Security: security/__init__.py
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 :-)