Spaces:
Runtime error

echem-agent / backend /app /api /users.py
SII-gelingli
Initial deployment to HF Spaces
ccab613
Raw History Blame Contribute Delete
1.74 kB
"""
Users API: profile info and settings.
"""
from fastapi import APIRouter, Depends, HTTPException
from pydantic import BaseModel, Field
from sqlalchemy.ext.asyncio import AsyncSession
from app.core.database import get_db
from app.core.security import verify_password, hash_password
from app.dependencies import get_current_user
from app.models.user import User
from app.schemas.auth import UserResponse
router = APIRouter(prefix="/users", tags=["users"])
class UpdateProfileRequest(BaseModel):
email: str | None = None
preferences: dict | None = None
class ChangePasswordRequest(BaseModel):
current_password: str
new_password: str = Field(..., min_length=6)
@router.get("/profile", response_model=UserResponse)
async def get_profile(user: User = Depends(get_current_user)):
return user
@router.patch("/profile", response_model=UserResponse)
async def update_profile(
req: UpdateProfileRequest,
user: User = Depends(get_current_user),
db: AsyncSession = Depends(get_db),
):
if req.email is not None:
user.email = req.email
if req.preferences is not None:
user.preferences = req.preferences
await db.flush()
await db.refresh(user)
await db.commit()
return user
@router.post("/change-password")
async def change_password(
req: ChangePasswordRequest,
user: User = Depends(get_current_user),
db: AsyncSession = Depends(get_db),
):
if not verify_password(req.current_password, user.password_hash):
raise HTTPException(status_code=400, detail="Current password is incorrect")
user.password_hash = hash_password(req.new_password)
await db.flush()
await db.commit()
return {"message": "Password changed successfully"}