
- Set up FastAPI application with MongoDB Motor driver - Implemented user registration, login, and logout with HTTP-only cookies - Added JWT token authentication and password hashing - Created user management endpoints for username updates and password changes - Structured application with proper separation of concerns (models, schemas, services, routes) - Added CORS configuration and health endpoints - Documented API endpoints and environment variables in README
47 lines
1.2 KiB
Python
47 lines
1.2 KiB
Python
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.responses import JSONResponse
|
|
import os
|
|
from dotenv import load_dotenv
|
|
|
|
from app.routes import auth, users
|
|
from app.db.connection import connect_to_mongo, close_mongo_connection
|
|
|
|
load_dotenv()
|
|
|
|
app = FastAPI(
|
|
title="User Authentication Service",
|
|
description="A simple CRUD app with user authentication using HTTP-only cookies",
|
|
version="1.0.0"
|
|
)
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
app.include_router(auth.router, prefix="/api/auth", tags=["authentication"])
|
|
app.include_router(users.router, prefix="/api/users", tags=["users"])
|
|
|
|
@app.on_event("startup")
|
|
async def startup_db_client():
|
|
await connect_to_mongo()
|
|
|
|
@app.on_event("shutdown")
|
|
async def shutdown_db_client():
|
|
await close_mongo_connection()
|
|
|
|
@app.get("/")
|
|
async def root():
|
|
return {
|
|
"title": "User Authentication Service",
|
|
"documentation": "/docs",
|
|
"health": "/health"
|
|
}
|
|
|
|
@app.get("/health")
|
|
async def health_check():
|
|
return {"status": "healthy", "service": "User Authentication Service"} |