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 fastapi import APIRouter, HTTPException
|
||||||
from pydantic import BaseModel
|
|
||||||
from datetime import timedelta
|
foods = [] # In-memory storage
|
||||||
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
|
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
class UserAuth(BaseModel):
|
@router.post("/generate-food")
|
||||||
username: str
|
async def generate_food_image(
|
||||||
password: str
|
food_name: str = "pizza",
|
||||||
|
quantity: int = 1
|
||||||
@router.post("/login")
|
|
||||||
async def login(
|
|
||||||
user_data: UserAuth,
|
|
||||||
db: Session = Depends(get_db)
|
|
||||||
):
|
):
|
||||||
"""User authentication endpoint"""
|
"""Food image generation endpoint"""
|
||||||
user = db.query(User).filter(User.username == user_data.username).first()
|
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")
|
generated_images = []
|
||||||
|
for _ in range(quantity):
|
||||||
# Generate token with expiration
|
image_id = len(foods) + 1
|
||||||
access_token = create_access_token(
|
foods.append({
|
||||||
data={"sub": user.id},
|
"id": image_id,
|
||||||
expires_delta=timedelta(hours=1)
|
"food_name": food_name,
|
||||||
)
|
"image_url": f"https://example.com/food/{image_id}.jpg"
|
||||||
|
})
|
||||||
|
generated_images.append(image_id)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"access_token": access_token,
|
"message": "Food images generated successfully",
|
||||||
"token_type": "bearer",
|
"food_name": food_name,
|
||||||
"user_id": user.id,
|
"generated_images": generated_images,
|
||||||
"username": user.username
|
"features": {
|
||||||
}
|
"image_quality": "high",
|
||||||
|
"expires_in": 3600
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user