Skip to content

Commit

Permalink
fix edge cases and type issues in API routes
Browse files Browse the repository at this point in the history
  • Loading branch information
Winston-Hsiao committed Aug 10, 2024
1 parent 304ad37 commit b53726e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
12 changes: 7 additions & 5 deletions store/app/routers/email_signup.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,19 @@ async def create_signup_token(data: EmailSignUpRequest) -> EmailSignUpResponse:
# GET: Retrieve Signup Token
@email_signup_router.get("/get/{id}", response_model=GetTokenResponse)
async def get_signup_token(id: str) -> GetTokenResponse:
async with EmailSignUpCrud() as crud: # Properly enter the context manager
async with EmailSignUpCrud() as crud:
signup_token = await crud.get_email_signup_token(id)
if not signup_token:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Token not found.")
return signup_token

# Map the EmailSignUpToken to GetTokenResponse
return GetTokenResponse(id=signup_token.id, email=signup_token.email)


# DELETE: Delete Signup Token
@email_signup_router.delete("/delete/{id}", response_model=DeleteTokenResponse)
async def delete_signup_token(id: str, crud: EmailSignUpCrud = Depends()) -> DeleteTokenResponse:
deleted = await crud.delete_email_signup_token(id)
if not deleted:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Token not found.")
await crud.delete_email_signup_token(id)

# The return value should be a DeleteTokenResponse, not None
return {"message": "Token deleted successfully."}
8 changes: 3 additions & 5 deletions store/app/routers/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,17 +218,15 @@ class LoginResponse(BaseModel):


@users_router.post("/login", response_model=LoginResponse)
async def login_user(
data: LoginRequest, user_crud: UserCrud = Depends()
) -> LoginResponse: # Added return type annotation
async def login_user(data: LoginRequest, user_crud: UserCrud = Depends()) -> LoginResponse:
async with user_crud:
# Fetch user by email
user = await user_crud.get_user_from_email(data.email)
if not user:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid email or password")

# Verify password
if not verify_password(data.password, user.hashed_password):
# Ensure `hashed_password` is not None before verifying
if user.hashed_password is None or not verify_password(data.password, user.hashed_password):
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid email or password")

token = new_uuid()
Expand Down

0 comments on commit b53726e

Please sign in to comment.