You've already forked RekomenciBackend
feat(): yandex and email sign in
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
from dishka import BaseScope, Provider, Scope, provide_all
|
||||
|
||||
from template_project.application.auth_identity.interactors.sign_up import AuthIdentityInteractor
|
||||
from template_project.application.auth_identity.interactors.sign_in import SignInInteractor
|
||||
from template_project.application.auth_identity.interactors.sign_up import SignUpInteractor
|
||||
|
||||
|
||||
class InteractorProvider(Provider):
|
||||
scope: BaseScope | None = Scope.REQUEST
|
||||
|
||||
interactors = provide_all(
|
||||
AuthIdentityInteractor,
|
||||
SignInInteractor,
|
||||
SignUpInteractor,
|
||||
)
|
||||
|
||||
@@ -3,8 +3,15 @@ from dishka.integrations.fastapi import DishkaRoute
|
||||
from fastapi import APIRouter, HTTPException, status
|
||||
from pydantic import BaseModel, SecretStr
|
||||
|
||||
from template_project.application.auth_identity.errors import AuthError, InvalidCodeError
|
||||
from template_project.application.auth_identity.interactors.sign_up import AuthIdentityInteractor
|
||||
from template_project.application.auth_identity.errors import (
|
||||
AuthError,
|
||||
InvalidCodeError,
|
||||
InvalidCredentialsError,
|
||||
UserAlreadyExistsError,
|
||||
UserNotFoundError,
|
||||
)
|
||||
from template_project.application.auth_identity.interactors.sign_in import SignInInteractor
|
||||
from template_project.application.auth_identity.interactors.sign_up import SignUpInteractor
|
||||
from template_project.application.common.containers import SecretString
|
||||
|
||||
router = APIRouter(route_class=DishkaRoute)
|
||||
@@ -22,14 +29,18 @@ class EmailSignUpResponse(BaseModel):
|
||||
@router.post("/auth/sign_up/email")
|
||||
async def sign_up_email(
|
||||
request: EmailSignUpRequest,
|
||||
interactor: FromDishka[AuthIdentityInteractor],
|
||||
interactor: FromDishka[SignUpInteractor],
|
||||
) -> EmailSignUpResponse:
|
||||
response_interactor = await interactor.sign_up_email(
|
||||
email=request.email, password=SecretString(request.password.get_secret_value())
|
||||
)
|
||||
return EmailSignUpResponse(
|
||||
access_token=response_interactor.access_token,
|
||||
)
|
||||
try:
|
||||
response_interactor = await interactor.sign_up_email(
|
||||
email=request.email, password=SecretString(request.password.get_secret_value())
|
||||
)
|
||||
except UserAlreadyExistsError as error:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT, detail="User with this email already exists"
|
||||
) from error
|
||||
|
||||
return EmailSignUpResponse(access_token=response_interactor.access_token)
|
||||
|
||||
|
||||
class YandexSignUpRequest(BaseModel):
|
||||
@@ -43,7 +54,7 @@ class YandexSignUpResponse(BaseModel):
|
||||
@router.post("/auth/sign_up/yandex")
|
||||
async def sign_up_yandex(
|
||||
request: YandexSignUpRequest,
|
||||
interactor: FromDishka[AuthIdentityInteractor],
|
||||
interactor: FromDishka[SignUpInteractor],
|
||||
) -> YandexSignUpResponse:
|
||||
try:
|
||||
response_interactor = await interactor.sign_up_yandex(code=request.code)
|
||||
@@ -52,3 +63,59 @@ async def sign_up_yandex(
|
||||
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Authentication failed") from error
|
||||
|
||||
return YandexSignUpResponse(access_token=response_interactor.access_token)
|
||||
|
||||
|
||||
class EmailSignInRequest(BaseModel):
|
||||
email: str
|
||||
password: SecretStr
|
||||
|
||||
|
||||
class EmailSignInResponse(BaseModel):
|
||||
access_token: str
|
||||
|
||||
|
||||
@router.post("/auth/sign_in/email")
|
||||
async def sign_in_email(
|
||||
request: EmailSignInRequest,
|
||||
interactor: FromDishka[SignInInteractor],
|
||||
) -> EmailSignInResponse:
|
||||
try:
|
||||
response_interactor = await interactor.sign_in_email(
|
||||
email=request.email, password=SecretString(request.password.get_secret_value())
|
||||
)
|
||||
|
||||
except UserNotFoundError as error:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="User not found") from error
|
||||
|
||||
except InvalidCredentialsError as error:
|
||||
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid credentials") from error
|
||||
|
||||
return EmailSignInResponse(access_token=response_interactor.access_token)
|
||||
|
||||
|
||||
class YandexSignInRequest(BaseModel):
|
||||
code: str
|
||||
|
||||
|
||||
class YandexSignInResponse(BaseModel):
|
||||
access_token: str
|
||||
|
||||
|
||||
@router.post("/auth/sign_in/yandex")
|
||||
async def sign_in_yandex(
|
||||
request: YandexSignInRequest,
|
||||
interactor: FromDishka[SignInInteractor],
|
||||
) -> YandexSignInResponse:
|
||||
try:
|
||||
response_interactor = await interactor.sign_in_yandex(code=request.code)
|
||||
|
||||
except InvalidCodeError as error:
|
||||
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid authorization code") from error
|
||||
|
||||
except AuthError as error:
|
||||
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Authentication failed") from error
|
||||
|
||||
except UserNotFoundError as error:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="User not found") from error
|
||||
|
||||
return YandexSignInResponse(access_token=response_interactor.access_token)
|
||||
|
||||
Reference in New Issue
Block a user