fix: change login endpoint from OAuth2PasswordRequestForm to accept JSON body

This commit is contained in:
TradeMate Dev
2026-05-14 17:03:24 +08:00
parent cc1766ef7d
commit 93f6ad306a
+7 -3
View File
@@ -56,13 +56,17 @@ async def register(data: RegisterRequest, db: AsyncSession = Depends(get_db)):
@router.post("/login", response_model=LoginResponse)
async def login(
form: OAuth2PasswordRequestForm = Depends(),
data: dict,
db: AsyncSession = Depends(get_db),
):
result = await db.execute(select(User).where(User.phone == form.username))
phone = data.get("username") or data.get("phone")
password = data.get("password")
if not phone or not password:
raise HTTPException(status_code=422, detail="phone and password required")
result = await db.execute(select(User).where(User.phone == phone))
user = result.scalar_one_or_none()
if not user or not verify_password(form.password, user.password_hash):
if not user or not verify_password(password, user.password_hash):
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid credentials",