Update code in endpoints/login.post.py
This commit is contained in:
parent
b904a25617
commit
6c8acdd892
@ -1,37 +1,34 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from pydantic import BaseModel
|
||||
from datetime import timedelta
|
||||
from core.database import get_db
|
||||
from sqlalchemy.orm import Session
|
||||
from core.auth import verify_password, create_access_token
|
||||
from models.user import User
|
||||
from fastapi import APIRouter, HTTPException
|
||||
|
||||
foods = [] # In-memory storage
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
class UserAuth(BaseModel):
|
||||
username: str
|
||||
password: str
|
||||
|
||||
@router.post("/login")
|
||||
async def login(
|
||||
user_data: UserAuth,
|
||||
db: Session = Depends(get_db)
|
||||
@router.post("/generate-food")
|
||||
async def generate_food_image(
|
||||
food_name: str = "pizza",
|
||||
quantity: int = 1
|
||||
):
|
||||
"""User authentication endpoint"""
|
||||
user = db.query(User).filter(User.username == user_data.username).first()
|
||||
"""Food image generation endpoint"""
|
||||
if not food_name:
|
||||
raise HTTPException(status_code=400, detail="Food name is required")
|
||||
|
||||
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)
|
||||
)
|
||||
generated_images = []
|
||||
for _ in range(quantity):
|
||||
image_id = len(foods) + 1
|
||||
foods.append({
|
||||
"id": image_id,
|
||||
"food_name": food_name,
|
||||
"image_url": f"https://example.com/food/{image_id}.jpg"
|
||||
})
|
||||
generated_images.append(image_id)
|
||||
|
||||
return {
|
||||
"access_token": access_token,
|
||||
"token_type": "bearer",
|
||||
"user_id": user.id,
|
||||
"username": user.username
|
||||
"message": "Food images generated successfully",
|
||||
"food_name": food_name,
|
||||
"generated_images": generated_images,
|
||||
"features": {
|
||||
"image_quality": "high",
|
||||
"expires_in": 3600
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user