Add POST endpoint for /login

This commit is contained in:
Backend IM Bot 2025-03-27 18:47:10 +00:00
parent 42ff4fa26b
commit b0dbe17976

View File

@ -1,37 +1,21 @@
from fastapi import APIRouter, Depends, HTTPException # Entity: FoodImage
from pydantic import BaseModel
from datetime import timedelta ```python
from core.database import get_db from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from core.auth import verify_password, create_access_token from core.database import get_db
from models.user import User from models.food_image import FoodImage
from schemas.food_image import FoodImageSchema, FoodImageCreate
from helpers.food_image_helpers import generate_food_image
router = APIRouter() router = APIRouter()
class UserAuth(BaseModel): @router.post("/food-images", status_code=201, response_model=FoodImageSchema)
username: str async def generate_food_photo(
password: str food_details: FoodImageCreate,
@router.post("/login")
async def login(
user_data: UserAuth,
db: Session = Depends(get_db) db: Session = Depends(get_db)
): ):
"""User authentication endpoint""" """Generate a photo of food based on provided details"""
user = db.query(User).filter(User.username == user_data.username).first() generated_image = generate_food_image(db, food_details)
return generated_image
if not user or not verify_password(user_data.password, user.hashed_password): ```
raise HTTPException(status_code=400, detail="Invalid credentials")
# Generate token with expiration
access_token = create_access_token(
data={"sub": user.id},
expires_delta=timedelta(hours=1)
)
return {
"access_token": access_token,
"token_type": "bearer",
"user_id": user.id,
"username": user.username
}