|
1 | | -from typing import Annotated |
| 1 | +import logging |
| 2 | +from typing import Annotated, Literal |
2 | 3 |
|
3 | | -from fastapi import APIRouter |
| 4 | +from fastapi import APIRouter, Query |
4 | 5 | from fastapi.params import Depends |
5 | 6 | from pydantic import BaseModel |
6 | 7 |
|
7 | 8 | from auth0.client import Auth0Client, get_auth0_client |
8 | 9 | from schemas.biocommons import AppId |
| 10 | +from schemas.responses import FieldError |
| 11 | + |
| 12 | +logger = logging.getLogger(__name__) |
9 | 13 |
|
10 | 14 | router = APIRouter(prefix="/utils", tags=["utils"]) |
11 | 15 |
|
12 | 16 |
|
| 17 | +def _check_username_exists(username: str, auth0_client: Auth0Client) -> bool: |
| 18 | + """Helper function to check if a username exists in Auth0.""" |
| 19 | + try: |
| 20 | + q = f'username:"{username}"' |
| 21 | + res = auth0_client.get_users(q=q) |
| 22 | + |
| 23 | + users = res if isinstance(res, list) else getattr(res, "users", []) |
| 24 | + target = username.lower() |
| 25 | + |
| 26 | + return any( |
| 27 | + getattr(u, "username", "").lower() == target |
| 28 | + for u in users |
| 29 | + if getattr(u, "username", None) |
| 30 | + ) |
| 31 | + except Exception as e: |
| 32 | + logger.warning(f"Error checking username existence: {e}") |
| 33 | + return False |
| 34 | + |
| 35 | + |
| 36 | +def _check_email_exists(email: str, auth0_client: Auth0Client) -> bool: |
| 37 | + """Helper function to check if an email exists in Auth0.""" |
| 38 | + try: |
| 39 | + email_results = auth0_client.search_users_by_email(email) |
| 40 | + return len(email_results) > 0 |
| 41 | + except Exception as e: |
| 42 | + logger.warning(f"Error checking email existence: {e}") |
| 43 | + return False |
| 44 | + |
| 45 | + |
| 46 | +def check_existing_user(username: str, email: str, auth0_client: Auth0Client) -> Literal["both", "email", "username"] | None: |
| 47 | + """Check if username or email already exists in Auth0. |
| 48 | +
|
| 49 | + Returns: |
| 50 | + - "username" if username exists |
| 51 | + - "email" if email exists |
| 52 | + - "both" if both exist |
| 53 | + - None if neither exists |
| 54 | + """ |
| 55 | + username_exists = _check_username_exists(username, auth0_client) |
| 56 | + email_exists = _check_email_exists(email, auth0_client) |
| 57 | + |
| 58 | + if username_exists and email_exists: |
| 59 | + return "both" |
| 60 | + elif username_exists: |
| 61 | + return "username" |
| 62 | + elif email_exists: |
| 63 | + return "email" |
| 64 | + return None |
| 65 | + |
| 66 | + |
13 | 67 | class RegistrationInfo(BaseModel): |
14 | 68 | app: AppId = "biocommons" |
15 | 69 |
|
16 | 70 |
|
17 | | -@router.get("/registration_info") |
| 71 | +class AvailabilityResponse(BaseModel): |
| 72 | + """Response for checking username/email availability""" |
| 73 | + available: bool |
| 74 | + field_errors: list[FieldError] = [] |
| 75 | + |
| 76 | + |
| 77 | +@router.get("/register/check-username-availability", response_model=AvailabilityResponse) |
| 78 | +async def check_username_availability( |
| 79 | + username: Annotated[str, Query(min_length=3, max_length=128)], |
| 80 | + auth0_client: Annotated[Auth0Client, Depends(get_auth0_client)], |
| 81 | +): |
| 82 | + """ |
| 83 | + Check if a username is available for registration. |
| 84 | +
|
| 85 | + Returns availability status with field errors if already taken. |
| 86 | + """ |
| 87 | + exists = _check_username_exists(username, auth0_client) |
| 88 | + if exists: |
| 89 | + return AvailabilityResponse( |
| 90 | + available=False, |
| 91 | + field_errors=[FieldError(field="username", message="Username is already taken")] |
| 92 | + ) |
| 93 | + return AvailabilityResponse(available=True) |
| 94 | + |
| 95 | + |
| 96 | +@router.get("/register/check-email-availability", response_model=AvailabilityResponse) |
| 97 | +async def check_email_availability( |
| 98 | + email: Annotated[str, Query()], |
| 99 | + auth0_client: Annotated[Auth0Client, Depends(get_auth0_client)], |
| 100 | +): |
| 101 | + """ |
| 102 | + Check if an email is available for registration. |
| 103 | +
|
| 104 | + Returns availability status with field errors if already registered. |
| 105 | + """ |
| 106 | + exists = _check_email_exists(email, auth0_client) |
| 107 | + if exists: |
| 108 | + return AvailabilityResponse( |
| 109 | + available=False, |
| 110 | + field_errors=[FieldError(field="email", message="Email is already taken")] |
| 111 | + ) |
| 112 | + return AvailabilityResponse(available=True) |
| 113 | + |
| 114 | + |
| 115 | +@router.get("/register/registration-info") |
18 | 116 | async def get_registration_info( |
19 | 117 | user_email: str, |
20 | 118 | client: Annotated[Auth0Client, Depends(get_auth0_client)]): |
|
0 commit comments