Spaces:
Running
Running
axeronis commited on
Commit ·
c67be1b
1
Parent(s): bdedbed
fix: change_password endpoint session handling to prevent duplicate key error
Browse files- backend/app/api/auth.py +14 -2
backend/app/api/auth.py
CHANGED
|
@@ -739,10 +739,22 @@ async def change_password(
|
|
| 739 |
status_code=status.HTTP_400_BAD_REQUEST,
|
| 740 |
detail="Incorrect current password"
|
| 741 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 742 |
|
| 743 |
# Update password
|
| 744 |
-
|
| 745 |
-
session.add(
|
| 746 |
await session.commit()
|
| 747 |
|
| 748 |
return {"message": "Password updated successfully"}
|
|
|
|
| 739 |
status_code=status.HTTP_400_BAD_REQUEST,
|
| 740 |
detail="Incorrect current password"
|
| 741 |
)
|
| 742 |
+
|
| 743 |
+
# Get user from current session (avoid detached object issue)
|
| 744 |
+
result = await session.execute(
|
| 745 |
+
select(User).where(User.id == current_user.id)
|
| 746 |
+
)
|
| 747 |
+
user = result.scalar_one_or_none()
|
| 748 |
+
|
| 749 |
+
if not user:
|
| 750 |
+
raise HTTPException(
|
| 751 |
+
status_code=status.HTTP_404_NOT_FOUND,
|
| 752 |
+
detail="User not found"
|
| 753 |
+
)
|
| 754 |
|
| 755 |
# Update password
|
| 756 |
+
user.hashed_password = User.get_password_hash(body.new_password)
|
| 757 |
+
session.add(user)
|
| 758 |
await session.commit()
|
| 759 |
|
| 760 |
return {"message": "Password updated successfully"}
|