@@ -686,6 +686,63 @@ async def reset_password(
686686    return  standard_responses .Result ()
687687
688688
689+ @router .post ( 
690+     "/users/change-password" , 
691+     response_model = standard_responses .Result , 
692+     status_code = 201 , 
693+ ) 
694+ async  def  change_password (
695+     change_password_request : schemas_users .ChangePasswordRequest ,
696+     db : AsyncSession  =  Depends (get_db ),
697+ ):
698+     """ 
699+     Change a user password. 
700+ 
701+     This endpoint will check the **old_password**, see also the `/users/reset-password` endpoint if the user forgot their password. 
702+     """ 
703+ 
704+     user  =  await  security .authenticate_user (
705+         db = db ,
706+         email = change_password_request .email ,
707+         password = change_password_request .old_password ,
708+     )
709+     if  user  is  None :
710+         raise  HTTPException (status_code = 403 , detail = "The old password is invalid" )
711+ 
712+     if  user .should_change_password :
713+         # we control whether we check if the new password is different 
714+         if  security .verify_password (
715+             change_password_request .new_password ,
716+             user .password_hash ,
717+         ):
718+             raise  HTTPException (
719+                 status_code = 403 ,
720+                 detail = "The new password should not be identical to the current password" ,
721+             )
722+ 
723+     new_password_hash  =  security .get_password_hash (change_password_request .new_password )
724+     await  cruds_users .update_user_password_by_id (
725+         db = db ,
726+         user_id = user .id ,
727+         new_password_hash = new_password_hash ,
728+     )
729+     await  cruds_users .update_should_user_change_password_by_id (
730+         db = db ,
731+         user_id = user .id ,
732+         should_change_password = False ,
733+     )
734+ 
735+     # Revoke existing auth refresh tokens 
736+     # to force the user to reauthenticate on all services and devices 
737+     # when their token expire 
738+     await  cruds_auth .revoke_refresh_token_by_user_id (
739+         db = db ,
740+         user_id = user .id ,
741+     )
742+ 
743+     return  standard_responses .Result ()
744+ 
745+ 
689746@router .post ( 
690747    "/users/migrate-mail" , 
691748    status_code = 204 , 
@@ -821,63 +878,6 @@ async def migrate_mail_confirm(
821878    return  "The email address has been successfully updated" 
822879
823880
824- @router .post ( 
825-     "/users/change-password" , 
826-     response_model = standard_responses .Result , 
827-     status_code = 201 , 
828- ) 
829- async  def  change_password (
830-     change_password_request : schemas_users .ChangePasswordRequest ,
831-     db : AsyncSession  =  Depends (get_db ),
832- ):
833-     """ 
834-     Change a user password. 
835- 
836-     This endpoint will check the **old_password**, see also the `/users/reset-password` endpoint if the user forgot their password. 
837-     """ 
838- 
839-     user  =  await  security .authenticate_user (
840-         db = db ,
841-         email = change_password_request .email ,
842-         password = change_password_request .old_password ,
843-     )
844-     if  user  is  None :
845-         raise  HTTPException (status_code = 403 , detail = "The old password is invalid" )
846- 
847-     if  user .should_change_password :
848-         # we control whether we check if the new password is different 
849-         if  security .verify_password (
850-             change_password_request .new_password ,
851-             user .password_hash ,
852-         ):
853-             raise  HTTPException (
854-                 status_code = 403 ,
855-                 detail = "The new password should not be identical to the current password" ,
856-             )
857- 
858-     new_password_hash  =  security .get_password_hash (change_password_request .new_password )
859-     await  cruds_users .update_user_password_by_id (
860-         db = db ,
861-         user_id = user .id ,
862-         new_password_hash = new_password_hash ,
863-     )
864-     await  cruds_users .update_should_user_change_password_by_id (
865-         db = db ,
866-         user_id = user .id ,
867-         should_change_password = False ,
868-     )
869- 
870-     # Revoke existing auth refresh tokens 
871-     # to force the user to reauthenticate on all services and devices 
872-     # when their token expire 
873-     await  cruds_auth .revoke_refresh_token_by_user_id (
874-         db = db ,
875-         user_id = user .id ,
876-     )
877- 
878-     return  standard_responses .Result ()
879- 
880- 
881881# We put the following endpoints at the end of the file to prevent them 
882882# from interacting with the previous endpoints 
883883# Ex: /users/activate is interpreted as a user whose id is "activate" 
0 commit comments