Jul 25, 2024
This lecture covers the creation of a full-stack application using Next.js for the UI and FastAPI as the backend. The project is structured in three primary steps:
Create Project Structure:
FastAPI.main.py.Virtual Environment:
python -m venv venv.Dependencies:
requirements.txt file with necessary dependencies.pip install -r requirements.txt.Application Structure:
API for business logic.main.py into the API directory.Basic FastAPI Setup:
app = FastAPI()).@app.get("/")
def health_check():
return {"message": "Health check complete"}
CORS Setup:
from fastapi.middleware.cors import CORSMiddleware
app.add_middleware(CORSMiddleware, allow_origins=["http://localhost:3000"], allow_credentials=True, allow_methods=["*"])
Database Initialization:
database.py file for SQLAlchemy database engine setup.from sqlalchemy import create_engine
engine = create_engine("sqlite:///app.db")
Define Models:
models.py file and define User, Workout, and Routine classes using SQLAlchemy.Add Initialization:
__init__.py in API folder for package structure.Define Dependencies:
dependencies.py file for handling session and configuration (e.g., JWT secret key, algorithm).Create Router:
routers folder to handle authentication routes.Functionality:
Link Everything Together:
main.py with app.include_router().Setup Next.js Application:
npx create-next-app nextjs.npm install axios bootstrap.Create Context for Authentication:
offContext.js inside a new context folder to manage user authentication state.Protected Route Management:
ProtectedRoute component to restrict access to certain routes based on authentication status.Login Functionality:
User Registration:
Fetching and Displaying Data: