Skip to content

Commit 4ed648a

Browse files
Added request models for user profile update
1 parent 9d8c5b3 commit 4ed648a

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

routers/user.py

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
# -- Server Request and Response Models --
1212

1313

14-
class UserProfile(BaseModel):
14+
class UpdateProfile(BaseModel):
15+
"""Request model for updating user profile information"""
1516
name: str
1617
email: EmailStr
1718
avatar_url: str
@@ -40,43 +41,40 @@ async def as_form(
4041
# -- Routes --
4142

4243

43-
@router.get("/profile", response_class=RedirectResponse)
44-
async def view_profile(
45-
current_user: User = Depends(get_authenticated_user)
46-
):
47-
# Render the profile page with the current user's data
48-
return {"user": current_user}
49-
50-
51-
@router.post("/edit_profile", response_class=RedirectResponse)
52-
async def edit_profile(
53-
name: str = Form(...),
54-
email: str = Form(...),
55-
avatar_url: str = Form(...),
44+
@router.post("/update_profile", response_class=RedirectResponse)
45+
async def update_profile(
46+
user_profile: UpdateProfile = Depends(UpdateProfile.as_form),
5647
current_user: User = Depends(get_authenticated_user),
5748
session: Session = Depends(get_session)
5849
):
5950
# Update user details
60-
current_user.name = name
61-
current_user.email = email
62-
current_user.avatar_url = avatar_url
51+
current_user.name = user_profile.name
52+
current_user.email = user_profile.email
53+
current_user.avatar_url = user_profile.avatar_url
6354
session.commit()
6455
session.refresh(current_user)
6556
return RedirectResponse(url="/profile", status_code=303)
6657

6758

6859
@router.post("/delete_account", response_class=RedirectResponse)
6960
async def delete_account(
70-
confirm_delete_password: str = Form(...),
61+
user_delete_account: UserDeleteAccount = Depends(
62+
UserDeleteAccount.as_form),
7163
current_user: User = Depends(get_authenticated_user),
7264
session: Session = Depends(get_session)
7365
):
74-
if not verify_password(confirm_delete_password, current_user.hashed_password):
75-
raise HTTPException(status_code=400, detail="Password is incorrect")
66+
if not verify_password(
67+
user_delete_account.confirm_delete_password,
68+
current_user.hashed_password
69+
):
70+
raise HTTPException(
71+
status_code=400,
72+
detail="Password is incorrect"
73+
)
7674

7775
# Delete the user
7876
session.delete(current_user)
7977
session.commit()
8078

8179
# Log out the user
82-
return RedirectResponse(url="/logout", status_code=303)
80+
return RedirectResponse(url="/auth/logout", status_code=303)

0 commit comments

Comments
 (0)