Skip to content

Commit a938219

Browse files
chore: refactor reset password to avoid duplicate code
1 parent b0f6195 commit a938219

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

backend/app/api/routes/login.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
from datetime import timedelta
22
from typing import Annotated, Any
33

4-
from fastapi import APIRouter, Depends, HTTPException
5-
from fastapi.responses import HTMLResponse
6-
from fastapi.security import OAuth2PasswordRequestForm
7-
84
from app import crud
95
from app.api.deps import CurrentUser, SessionDep, get_current_active_superuser
106
from app.core import security
117
from app.core.config import settings
128
from app.core.security import get_password_hash
139
from app.models import Message, NewPassword, Token, UserPublic
14-
from app.utils import (
15-
generate_password_reset_token,
16-
generate_reset_password_email,
17-
send_email,
18-
verify_password_reset_token,
19-
)
10+
from app.utils import (generate_password_reset_token,
11+
generate_reset_password_email, send_email,
12+
verify_password_reset_token)
13+
from fastapi import APIRouter, Depends, HTTPException
14+
from fastapi.responses import HTMLResponse
15+
from fastapi.security import OAuth2PasswordRequestForm
2016

2117
router = APIRouter(tags=["login"])
2218

@@ -86,15 +82,16 @@ def reset_password(session: SessionDep, body: NewPassword) -> Message:
8682
user = crud.get_user_by_email(session=session, email=email)
8783
if not user:
8884
raise HTTPException(
89-
status_code=404,
90-
detail="The user with this email does not exist in the system.",
85+
status_code=404, detail="The user with this email does not exist."
9186
)
92-
elif not user.is_active:
87+
if not user.is_active:
9388
raise HTTPException(status_code=400, detail="Inactive user")
94-
hashed_password = get_password_hash(password=body.new_password)
95-
user.hashed_password = hashed_password
96-
session.add(user)
97-
session.commit()
89+
90+
crud.update_user(
91+
session=session,
92+
db_user=user,
93+
user_in={"password": body.new_password},
94+
)
9895
return Message(message="Password updated successfully")
9996

10097

0 commit comments

Comments
 (0)