MCPcopy Create free account
hub / github.com/dbunt1tled/python-fast-api / AuthController

Class AuthController

src/app/auth/controller/auth_controller.py:17–75  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

15
16
17class AuthController(BaseController):
18 def __init__(self, app: FastAPI, container: Container) -> None:
19 super().__init__(container=container)
20 router = APIRouter(prefix="/auth", tags=["auth"])
21 router.add_api_route(path="/login", endpoint=self.login, methods=["POST"])
22 router.add_api_route(path="/signup", endpoint=self.signup, methods=["POST"])
23 router.add_api_route(path="/re-send-confirm-email", endpoint=self.re_send_confirm_email, methods=["POST"])
24 router.add_api_route(path="/confirm-email", endpoint=self.confirm_user, methods=["GET"])
25 router.add_api_route(path="/refresh", endpoint=self.refresh, methods=["POST"])
26 app.include_router(router=router)
27
28 async def login(self, req: LoginRequest) -> JsonApiResponse:
29 token = await self.container.auth_service().login(email=req.email, password=req.password)
30
31 return await self.response(data=Bearer.from_token(token))
32
33 async def signup(self, req: SignupRequest) -> JsonApiResponse:
34 user = await self.container.auth_service().signup(
35 first_name=req.first_name, second_name=req.second_name, email=req.email, password=req.password
36 )
37
38 return await self.response(data=user)
39
40 async def re_send_confirm_email(self, req: ReSendConfirmEmailRequest) -> JsonApiResponse:
41 res = Message(message="Email successfully sent")
42 user = await self.container.user_service().one(
43 filters=[
44 Filter("email", Oper.EQ, req.email),
45 ]
46 )
47 if user is None:
48 return await self.response(data=res)
49
50 return await self.response(data=res)
51
52 async def confirm_user(self, req: ConfirmUserRequest = Depends()) -> JsonApiResponse:
53 await self.container.auth_service().confirm_user(jwt=req.token)
54
55 return await self.response(data=Message(message="Email successfully confirmed"))
56
57 async def refresh(self, authorization: str = Header(None)) -> JsonApiResponse:
58 if authorization is None:
59 raise UnauthorizedException(error_no=ErrorNo.AUTHORIZATION_REFRESH_TOKEN_EMPTY, message="Unauthorized!")
60 try:
61 scheme, jwt = authorization.split(" ", 1)
62 except ValueError as e:
63 self.log().error(str(e), exc_info=e)
64 raise UnauthorizedException(
65 error_no=ErrorNo.AUTHORIZATION_REFRESH_TOKEN_INVALID, message="Unauthorized!", inner_exception=e
66 )
67
68 if scheme.lower() != "bearer":
69 raise UnauthorizedException(
70 error_no=ErrorNo.AUTHORIZATION_REFRESH_TOKEN_FORMAT_ERROR, message="Unauthorized!"
71 )
72
73 token = await self.container.auth_service().refresh(jwt=jwt)
74

Callers 1

lifespanFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected