from fastapi import BackgroundTasks, Security, APIRouter from fastapi_jwt import JwtAuthorizationCredentials from starlette.responses import Response from endpoints.login import LoginInput, LoginEndpoint, RefreshEndpoint, LogoutEndpoint from endpoints.registration import RegistrationInput, RegistrationEndpoint from jwt import access_security, refresh_security router = APIRouter() @router.post("/registration", status_code=201) async def registration(user: RegistrationInput, background_tasks: BackgroundTasks): return await RegistrationEndpoint().on_post(user, background_tasks) @router.post("/login") async def login(resp: Response, user: LoginInput): return await LoginEndpoint( access_token=access_security, refresh_token=refresh_security ).on_post(user, resp) @router.post("/refresh", summary="Refresh access token") async def refresh( resp: Response, credentials: JwtAuthorizationCredentials = Security(refresh_security) ): return await RefreshEndpoint( access_token=access_security, refresh_token=refresh_security ).on_post(resp, credentials) @router.post("/logout") async def logout(resp: Response): return await LogoutEndpoint( access_token=access_security, refresh_token=refresh_security ).on_post(resp)