29 lines
919 B
Python
29 lines
919 B
Python
from fastapi import APIRouter, Depends, HTTPException
|
|
from fastapi.security import OAuth2PasswordRequestForm
|
|
|
|
from ..di import get_auth_service, get_user_service
|
|
from ..schemas import TokenSchema, UserReadDTO, UserWriteDTO
|
|
from ..services import AuthService, UserService
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.post("/token", response_model=TokenSchema, tags=["auth"])
|
|
async def authenticate(
|
|
login: OAuth2PasswordRequestForm = Depends(),
|
|
auth: AuthService = Depends(get_auth_service),
|
|
) -> TokenSchema | None:
|
|
return await auth.authenticate(login)
|
|
|
|
|
|
@router.post("/register", response_model=UserReadDTO, tags=["auth"], status_code=201)
|
|
async def register(
|
|
data: UserWriteDTO,
|
|
user_service: UserService = Depends(get_user_service),
|
|
) -> UserReadDTO:
|
|
res = await user_service.add_one(data)
|
|
if not isinstance(res, UserReadDTO):
|
|
raise HTTPException(status_code=400, detail=res)
|
|
|
|
return res
|