Skip to content

Commit

Permalink
🛠 chore : Fix validation error for Msg #7
Browse files Browse the repository at this point in the history
  • Loading branch information
yezz123 committed Aug 19, 2021
1 parent 5f4d74e commit b5804a0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 22 deletions.
24 changes: 11 additions & 13 deletions api/endpoints/login.py
Expand Up @@ -8,8 +8,8 @@
from fastapi.security import OAuth2PasswordRequestForm
from sqlalchemy.orm import Session

import crud
import models
from crud import crud_user
from crud.crud_user import CRUDUser
from api import deps
from core import security
Expand Down Expand Up @@ -60,19 +60,22 @@ def Check_Session(current_user: models.user.User = Depends(
return current_user


@router.post("/password-recovery/{email}", response_model=Msg)
@router.post("/password-recovery/{email}",
response_model=Msg,
status_code=200,
response_description="Success")
def recover_password(email: str, db: Session = Depends(deps.get_db)) -> Any:
"""
Password Recovery
"""
user = user1.get_by_email(db, email=email)

if not user:
raise HTTPException(
status_code=404,
status_code=422,
detail="The user with this username does not exist in the system.",
)
password_reset_token = generate_password_reset_token(email=email)
return {"Message": "Password recovery email sent"}
return {"msg": generate_password_reset_token(email=email)}


@router.post("/reset-password/", response_model=Msg)
Expand All @@ -87,16 +90,11 @@ def reset_password(
email = verify_password_reset_token(token)
if not email:
raise HTTPException(status_code=400, detail="Invalid token")
user = crud.user.get_by_email(db, email=email)
user = crud_user.CRUDUser.get_by_email(db, email=email)
if not user:
raise HTTPException(
status_code=404,
detail="The user with this username does not exist in the system.",
)
elif not crud.user.is_active(user):
raise HTTPException(status_code=400, detail="Inactive user")
raise HTTPException(status_code=404, detail="User not found")
hashed_password = get_password_hash(new_password)
user.hashed_password = hashed_password
db.add(user)
db.commit()
return {"Message": "Password updated successfully"}
return {"msg": "Password updated successfully!"}
4 changes: 1 addition & 3 deletions core/security.py
Expand Up @@ -17,9 +17,7 @@ def create_access_token(subject: Union[str, Any],
expire = datetime.utcnow() + timedelta(
minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
to_encode = {"exp": expire, "sub": str(subject)}
return jwt.encode(to_encode,
settings.SECRET_KEY,
algorithm=ALGORITHM)
return jwt.encode(to_encode, settings.SECRET_KEY, algorithm=ALGORITHM)


def verify_password(plain_password: str, hashed_password: str) -> bool:
Expand Down
8 changes: 2 additions & 6 deletions utils.py
Expand Up @@ -9,7 +9,7 @@ def generate_password_reset_token(email: str) -> str:
now = datetime.utcnow()
expires = now + delta
exp = expires.timestamp()
encoded_jwt = jwt.encode(
return jwt.encode(
{
"exp": exp,
"nbf": now,
Expand All @@ -18,14 +18,10 @@ def generate_password_reset_token(email: str) -> str:
settings.SECRET_KEY,
algorithm="HS256",
)
return encoded_jwt


def verify_password_reset_token(token: str) -> Optional[str]:
try:
decoded_token = jwt.decode(token,
settings.SECRET_KEY,
algorithms=["HS256"])
return decoded_token["email"]
return jwt.decode(token, settings.SECRET_KEY, algorithms=["HS256"])
except jwt.JWTError:
return None

0 comments on commit b5804a0

Please sign in to comment.