feat: add set phone

This commit is contained in:
2026-05-14 21:45:43 +03:00
parent 6465807394
commit 75362b07ae
10 changed files with 105 additions and 29 deletions

View File

@@ -1,7 +1,7 @@
from fastapi import Depends, Request
from fastapi.security.utils import get_authorization_scheme_param
from src.application.contracts import IJwtService
from src.application.domain.exceptions import ApplicationException
from src.application.domain.exceptions import UnauthorizedException
from src.application.domain.dto import AccessTokenPayload, AuthContext
from src.presentation.dependencies import get_jwt_service
@@ -27,10 +27,10 @@ async def require_access_token(
) -> AuthContext:
token = _extract_access_token(request)
if not token:
raise ApplicationException(status_code=401, message='Not authenticated')
raise UnauthorizedException(message='Not authenticated')
payload: AccessTokenPayload = await jwt_service.decode_access_token(token)
if payload.type != 'access':
raise ApplicationException(status_code=401, message='Invalid token type')
raise UnauthorizedException(message='Invalid token type')
return AuthContext(user_id=payload.sub, sid=payload.sid, token=payload)

View File

@@ -1,2 +1,4 @@
from src.presentation.handlers.unhandled_handler import unhandled_exception_handler
from src.presentation.handlers.application_handler import application_exception_handler
from src.presentation.handlers.application_handler import application_exception_handler
from src.presentation.handlers.http_exception_handler import http_exception_handler
from src.presentation.handlers.validation_handler import validation_exception_handler

View File

@@ -0,0 +1,11 @@
from fastapi import Request
from fastapi.responses import ORJSONResponse
from starlette.exceptions import HTTPException
async def http_exception_handler(_request: Request, exc: HTTPException) -> ORJSONResponse:
return ORJSONResponse(
status_code=exc.status_code,
content={'detail': exc.detail},
headers=dict(exc.headers) if exc.headers else None,
)

View File

@@ -0,0 +1,10 @@
from fastapi import Request
from fastapi.exceptions import RequestValidationError
from fastapi.responses import ORJSONResponse
async def validation_exception_handler(_request: Request, exc: RequestValidationError) -> ORJSONResponse:
return ORJSONResponse(
status_code=422,
content={'detail': exc.errors()},
)

View File

@@ -1,13 +1,12 @@
from fastapi import APIRouter
from src.presentation.routing.account import account_router
from src.presentation.routing.account_settings import account_settings_router
me_router = APIRouter(prefix='/me', tags=['Account'])
me_router.include_router(account_router)
# from src.presentation.routing.account_settings import account_settings_router
# me_router.include_router(account_settings_router)
me_router.include_router(account_settings_router)
# from src.presentation.routing.devices import devices_router
# me_devices_router = APIRouter(prefix='/me', tags=['Devices'])

View File

@@ -1,6 +1,5 @@
import re
from pydantic import BaseModel, field_validator
from src.application.domain.exceptions import ApplicationException
class SetPhoneRequest(BaseModel):
@@ -12,6 +11,6 @@ class SetPhoneRequest(BaseModel):
cleaned = re.sub(r'[\s\-\(\)]', '', v)
pattern = r'^(\+7|8)\d{10}$'
if not re.match(pattern, cleaned):
raise ApplicationException(message='Invalid Russian phone number', status_code=429)
raise ValueError('Invalid Russian phone number')
normalized = '+7' + cleaned[-10:]
return normalized
return normalized