forked from cartsnitch/cartsnitch
27fe957074
Consolidate API gateway service into monorepo. Squashed from https://github.com/cartsnitch/api main (89bacb1). Co-Authored-By: Paperclip <noreply@paperclip.ing>
97 lines
3.0 KiB
Python
97 lines
3.0 KiB
Python
"""Auth routes: register, login, refresh, me, update, delete."""
|
|
|
|
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 (
|
|
LoginRequest,
|
|
RefreshRequest,
|
|
RegisterRequest,
|
|
TokenResponse,
|
|
UpdateUserRequest,
|
|
UserResponse,
|
|
)
|
|
from cartsnitch_api.services.auth import AuthService
|
|
|
|
router = APIRouter(prefix="/auth", tags=["auth"])
|
|
|
|
|
|
@router.post("/register", response_model=TokenResponse, status_code=status.HTTP_201_CREATED)
|
|
async def register(body: RegisterRequest, db: AsyncSession = Depends(get_db)):
|
|
svc = AuthService(db)
|
|
try:
|
|
return await svc.register(body.email, body.password, body.display_name)
|
|
except ValueError as e:
|
|
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail=str(e)) from e
|
|
|
|
|
|
@router.post("/login", response_model=TokenResponse)
|
|
async def login(body: LoginRequest, db: AsyncSession = Depends(get_db)):
|
|
svc = AuthService(db)
|
|
try:
|
|
return await svc.login(body.email, body.password)
|
|
except ValueError:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid email or password"
|
|
) from None
|
|
|
|
|
|
@router.post("/refresh", response_model=TokenResponse)
|
|
async def refresh(body: RefreshRequest, db: AsyncSession = Depends(get_db)):
|
|
svc = AuthService(db)
|
|
try:
|
|
return await svc.refresh(body.refresh_token)
|
|
except ValueError:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid refresh token"
|
|
) from None
|
|
|
|
|
|
@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
|