Skip to content

Commit

Permalink
refactor(backend): handle user with wrong login method
Browse files Browse the repository at this point in the history
  • Loading branch information
tuantran0910 committed Jan 8, 2025
1 parent 8d21f4d commit fe796c6
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
10 changes: 6 additions & 4 deletions chatbot-core/backend/app/routers/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from app.settings.constants import Constants
from app.utils.api.api_response import APIResponse
from app.utils.api.api_response import BackendAPIResponse
from app.utils.api.error_handler import ErrorCodesMappingNumber
from app.utils.api.helpers import get_logger
from app.utils.user.jwt import create_access_token

Expand Down Expand Up @@ -49,11 +50,12 @@ def get_oauth_access_token(code: str, db_session: Session = Depends(get_db_sessi
if err:
status_code, detail = err.kind
raise HTTPException(status_code=status_code, detail=detail)

if not user.is_oauth:
elif not user.is_oauth:
# If the user already exists with a different login method, return an error
status_code, detail = ErrorCodesMappingNumber.USER_WRONG_LOGIN_METHOD.value
raise HTTPException(
status_code=status.HTTP_405_METHOD_NOT_ALLOWED,
detail=Constants.USER_WRONG_LOGIN_METHOD,
status_code=status_code,
detail=detail,
)

access_token = create_access_token(data=user_oauth_data.get("email"))
Expand Down
1 change: 0 additions & 1 deletion chatbot-core/backend/app/settings/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class Constants:
FORBIDDEN_REQUEST_MESSAGE = "Forbidden request"
NOT_FOUND_MESSAGE = "Not found"
USER_NOT_FOUND_MESSAGE = "User not found"
USER_WRONG_LOGIN_METHOD = "Wrong login method"
INTERNAL_SERVER_ERROR_MESSAGE = "Internal server error"
EMPTY_CHAT_MESSAGE_MESSAGE = "Empty chat message"

Expand Down
2 changes: 2 additions & 0 deletions chatbot-core/backend/app/utils/api/error_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class ErrorCodesMappingNumber(Enum):
EMBEDDING_PROVIDER_NOT_FOUND = (422, "Embedding provider not found")
PROVIDER_TYPE_CHANGE_NOT_ALLOWED = (422, "Provider type change not allowed")

USER_WRONG_LOGIN_METHOD = (405, "User already exists with wrong login method")


class BaseException(Exception):
"""
Expand Down

0 comments on commit fe796c6

Please sign in to comment.