Не могу настроить так, чтобы авторизация просходила нормально и без каких-то костылей
почему в swagger авторизация как отдельное поле, хотя должно все автоматически подставляться
async def get_current_user(
authorization: str = Header(..., alias="Authorization") # ← Уберите Optional
):
logger.debug(f"Authorization header: {authorization}")
credentials_exception = HTTPException(
status_code=401,
detail="Could not validate credentials",
headers={"WWW-Authenticate": "Bearer"},
)
if not authorization:
raise credentials_exception
if not authorization.startswith("Bearer "): # Проверяем префикс Bearer
raise credentials_exception
token = authorization[7:] # Убираем "Bearer "
try:
user_id = await verify_token(token, credentials_exception)
return user_id
except JWTError:
raise credentials_exception
@router.post("/change-password")
async def change_password(
old_password: str,
new_password: str,
user_id: str = Depends(get_current_user),
session: AsyncSession = Depends(get_session)
):
user = await user_repository.get_user_by_user_id(user_id, session)
user = await user_repository.get_user_by_user_id(user_id, session)
if not user:
raise HTTPException(status_code=404, detail="User not found")
if not await verify_password(old_password, user.hashed_password):
raise HTTPException(status_code=400, detail="Invalid old password")
user.hashed_password = await get_password_hash(new_password)
await user_repository.update_user(user, session)
return {"message": "Password changed successfully"}