@@ -118,10 +118,11 @@ async def as_form(
118
118
119
119
class UserResetPassword (BaseModel ):
120
120
email : EmailStr
121
- token : Optional [ str ]
121
+ token : str
122
122
new_password : str
123
123
confirm_new_password : str
124
124
125
+ # Use the factory with a different field name
125
126
validate_password_strength = create_password_validator ("new_password" )
126
127
validate_passwords_match = create_passwords_match_validator (
127
128
"new_password" , "confirm_new_password" )
@@ -130,16 +131,12 @@ class UserResetPassword(BaseModel):
130
131
async def as_form (
131
132
cls ,
132
133
email : EmailStr = Form (...),
133
- token : str = Form (None ),
134
+ token : str = Form (... ),
134
135
new_password : str = Form (...),
135
136
confirm_new_password : str = Form (...)
136
137
):
137
- return cls (
138
- email = email ,
139
- token = token ,
140
- new_password = new_password ,
141
- confirm_new_password = confirm_new_password
142
- )
138
+ return cls (email = email , token = token ,
139
+ new_password = new_password , confirm_new_password = confirm_new_password )
143
140
144
141
145
142
class UpdateEmail (BaseModel ):
@@ -318,39 +315,8 @@ async def forgot_password(
318
315
@router .post ("/reset_password" )
319
316
async def reset_password (
320
317
user : UserResetPassword = Depends (UserResetPassword .as_form ),
321
- tokens : tuple [Optional [str ], Optional [str ]] = Depends (oauth2_scheme_cookie ),
322
318
session : Session = Depends (get_session )
323
319
):
324
- access_token , _ = tokens
325
-
326
- # Handle authenticated user
327
- if access_token :
328
- try :
329
- decoded_token = validate_token (access_token )
330
- if decoded_token and decoded_token .get ("sub" ) == user .email :
331
- # User is authenticated and changing their own password
332
- db_user = session .exec (select (User ).where (
333
- User .email == user .email )).first ()
334
- if not db_user :
335
- raise HTTPException (status_code = 404 , detail = "User not found" )
336
-
337
- # Update password
338
- if db_user .password :
339
- db_user .password .hashed_password = get_password_hash (user .new_password )
340
- else :
341
- db_user .password = UserPassword (
342
- hashed_password = get_password_hash (user .new_password )
343
- )
344
- session .commit ()
345
- return RedirectResponse (url = "/settings" , status_code = 303 )
346
-
347
- except Exception as e :
348
- logger .error (f"Error validating token: { e } " )
349
-
350
- # Handle unauthenticated user with reset token
351
- if not user .token :
352
- raise HTTPException (status_code = 400 , detail = "Reset token required for unauthenticated password reset" )
353
-
354
320
authorized_user , reset_token = get_user_from_reset_token (
355
321
user .email , user .token , session )
356
322
@@ -363,13 +329,16 @@ async def reset_password(
363
329
user .new_password
364
330
)
365
331
else :
332
+ logger .warning (
333
+ "User password not found during password reset; creating new password for user" )
366
334
authorized_user .password = UserPassword (
367
335
hashed_password = get_password_hash (user .new_password )
368
336
)
369
337
370
338
reset_token .used = True
371
339
session .commit ()
372
-
340
+ session .refresh (authorized_user )
341
+
373
342
return RedirectResponse (url = "/login" , status_code = 303 )
374
343
375
344
0 commit comments