FastAPI-Vue-OAuth2
FastAPI-Vue-OAuth2 copied to clipboard
FastAPI Vue OAuth2 is a boilerplate for building a FastAPI backend with a Vue frontend. It includes OAuth2 authentication with JWT tokens, and a simple user CRUD. Project can be easily deployed with D...
FastAPI Vue OAuth2 Boilerplate
This boilerplate is a starting point for building a FastAPI backend using PostgreSQL with a Vue3 frontend.
It includes OAuth2 authentication with JWT tokens, and a simple user CRUD.
Note : For
Vue: Option API+VueXversion, please check out archived-2023-11-22 branch
Demo
localhost for frontend
localhost:5001/docs for backend swagger docs
Click image to watch demo video on YouTube ☝️
Features
- FastAPI backend with PostgreSQL database
- SQLAlchemy CRUD with async support
- Simple User CRUD
- OAuth2 authentication with JWT tokens
- Store refresh token in
httpOnlycookie, access token in memory ( Pinia store ) - Vue3 frontend with Pinia store
- Docker Compose for development and production
Project Structure & Details
Backend
app.pyFastAPI application files/apiAPI endpoints/auth- OAuth2 authentication
get_current_userdependency
/crud- user related CRUD utilities
- database session dependency
/databaseDatabase configuration files/modelsSQLAlchemy models usingdeclarative_base/schemasPydantic schemas
Database
PostgreSQL 15.1image from Docker Hub- exposed on port
5432 - volume
postgres_datafor persistent data
Frontend
ViteFrontend build tool/viewsFrontend page views- use
RefreshView.vueas middleware to refresh JWT tokens
- use
/storePinia store ( usingData Provider Patten)/routerVue router/apiAPI endpointsreq.jsaxiosrequest wrapper , handle401unauthorized error to refresh JWT tokens- use
import.meta.env.VITE_APP_API_URLto load API url from.envfile
Environment Variables
-
.envfor postgres databasePOSTGRES_USERPOSTGRES_PASSWORDPOSTGRES_DB
-
backend/.envfor backendPORTRELOADDATABASE_URLShould be same as above setting dot fileJWT_ALGORITHMACCESS_TOKEN_SECRETREFRESH_TOKEN_SECRETACCESS_TOKEN_EXPIRE_MINUTESREFRESH_TOKEN_EXPIRE_MINUTES
-
nginx/nginx.conffor nginx server- Note : backend hostname should be same as
docker-compose.ymlservice name
- Note : backend hostname should be same as
-
frontend/.envfor development API url -
frontend/.env.productionfor production API url
Deployment
Containerization
docker-compose.ymlDocker Compose configuration fileDockerfileDockerfile for frontend nginx server with production buildbackend/DockerfileDockerfile for backend with hot reload
Production
docker-compose up -d --build
Development
- Database
docker run --name fastapi_vue_oauth2_postgresql -e POSTGRES_USER=fastapi_vue_user -e POSTGRES_PASSWORD=fastapi_vue_password -e POSTGRES_DB=fastapi_vue_dev -p 5432:5432 -d -v postgres_data_dev:/var/lib/postgresql/data postgres:15.1
- Backend
Note : shuold change in changeDATABASE_URLtoDEV_DATABASE_URLinbackend/.env
- Poetry
cd backend poetry install poetry shell python3 run.py- Create virtual environment
cd backend python3 -m venv venv source venv/bin/activate pip3 install -r requirements.txt python3 run.py- Frontend
cd frontend yarn dev
Advanced : Kubernetes
Still working on it on features/k8s branch !
Issues & PR
Feel free to open an issue !
Pull requests are welcome.
Any contributions you make are greatly appreciated.