forked from cartsnitch/api
18ff5795ac
- Revert auth/dependencies.py, auth/routes.py, services/auth.py, schemas.py to Better-Auth session-cookie auth (removed JWT register/login/refresh) - Preserve GET /auth/me/email-in-address endpoint - Fix UUIDString TypeDecorator: process_result_value returns uuid.UUID (not str) so SQLAlchemy 2.0 sentinel tracking matches UUID-to-UUID - Fix seed_data fixture: look up real user_id from session token via sessions table; purchases now reference actual user FK - Update purchase_data fixture to use session-cookie auth - Update test_auth_endpoints, test_auth_validation to cookie-based tests - Remove TestRegistrationErrors and TestLoginErrors (no longer applicable) - Update test_openapi.py expected routes and count - Update test_error_handler.py to use PATCH /auth/me validation Co-Authored-By: Paperclip <noreply@paperclip.ing>
82 lines
2.5 KiB
Python
82 lines
2.5 KiB
Python
"""Auth routes: user profile management.
|
|
|
|
Registration, login, refresh, and session management are handled by
|
|
the Better-Auth service (auth/). This router provides user profile
|
|
endpoints that query our own user data from the shared database.
|
|
"""
|
|
|
|
from uuid import UUID
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from cartsnitch_api.auth.dependencies import get_current_user
|
|
from cartsnitch_api.database import get_db
|
|
from cartsnitch_api.schemas import (
|
|
EmailInAddressResponse,
|
|
UpdateUserRequest,
|
|
UserResponse,
|
|
)
|
|
from cartsnitch_api.services.auth import AuthService
|
|
|
|
router = APIRouter(prefix="/auth", tags=["auth"])
|
|
|
|
|
|
@router.get("/me", response_model=UserResponse)
|
|
async def get_me(
|
|
user_id: UUID = Depends(get_current_user),
|
|
db: AsyncSession = Depends(get_db),
|
|
):
|
|
svc = AuthService(db)
|
|
try:
|
|
return await svc.get_user(user_id)
|
|
except LookupError:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND, detail="User not found"
|
|
) from None
|
|
|
|
|
|
@router.patch("/me", response_model=UserResponse)
|
|
async def update_me(
|
|
body: UpdateUserRequest,
|
|
user_id: UUID = Depends(get_current_user),
|
|
db: AsyncSession = Depends(get_db),
|
|
):
|
|
svc = AuthService(db)
|
|
try:
|
|
return await svc.update_user(user_id, email=body.email, display_name=body.display_name)
|
|
except LookupError:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND, detail="User not found"
|
|
) from None
|
|
except ValueError as e:
|
|
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail=str(e)) from e
|
|
|
|
|
|
@router.delete("/me", status_code=status.HTTP_204_NO_CONTENT)
|
|
async def delete_me(
|
|
user_id: UUID = Depends(get_current_user),
|
|
db: AsyncSession = Depends(get_db),
|
|
):
|
|
svc = AuthService(db)
|
|
try:
|
|
await svc.delete_user(user_id)
|
|
except LookupError:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND, detail="User not found"
|
|
) from None
|
|
|
|
|
|
@router.get("/me/email-in-address", response_model=EmailInAddressResponse)
|
|
async def get_email_in_address(
|
|
user_id: UUID = Depends(get_current_user),
|
|
db: AsyncSession = Depends(get_db),
|
|
):
|
|
svc = AuthService(db)
|
|
try:
|
|
return await svc.get_email_in_address(user_id)
|
|
except LookupError:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND, detail="Email inbound token not found"
|
|
) from None
|