diff --git a/app/api/auth/routes.py b/app/api/auth/routes.py index 441f6eb..1ae1898 100644 --- a/app/api/auth/routes.py +++ b/app/api/auth/routes.py @@ -1,14 +1,14 @@ from fastapi import APIRouter, Depends, Path, Request, HTTPException from api.auth.schema import LoginToken, LoginResponse, BingoUser, LoginType, LoginUrl from api.auth.services.social_login import SocialLoginDepends -from api.auth.services.bingo_login import CreateBingoUser, GetBingoUserByName, GetBingoUserById +from api.auth.services.bingo_login import LoginUser, GetBingoUserByName, GetBingoUserById auth_router = APIRouter(prefix="/auth") @auth_router.post("/bingo/sign-up", response_model=BingoUser, description="빙고용 임시 회원가입 API") -async def bingo_sign_up(username: str, bingo_user: CreateBingoUser = Depends(CreateBingoUser)): - res = await bingo_user.execute(username) +async def bingo_sign_up(username: str, password: str, bingo_user: LoginUser = Depends(LoginUser)): + res = await bingo_user.execute(username, password) return res diff --git a/app/api/auth/services/bingo_login.py b/app/api/auth/services/bingo_login.py index 1ed4b3a..1baccf8 100644 --- a/app/api/auth/services/bingo_login.py +++ b/app/api/auth/services/bingo_login.py @@ -8,10 +8,14 @@ def __init__(self, session: AsyncSessionDepends): self.async_session = session -class CreateBingoUser(BaseBingoUser): - async def execute(self, username: str) -> BingoUser: +class LoginUser(BaseBingoUser): + async def execute(self, username: str, password: str) -> BingoUser: try: - user = await BingoUser.create(self.async_session, username) + user = await BingoUser.get_user_by_name(self.async_session, username) + if not user: + user = await BingoUser.create(self.async_session, username, password) + elif password != user.password: + raise ValueError("password가 잘못되었습니다.") return BingoUserResponse(**user.__dict__, ok=True, message="빙고 유저 생성에 성공하였습니다.") except ValueError as e: return BingoUserResponse(ok=False, message=str(e)) diff --git a/app/models/user.py b/app/models/user.py index 244101b..c9f0e7c 100644 --- a/app/models/user.py +++ b/app/models/user.py @@ -12,17 +12,18 @@ class BingoUser(Base): __tablename__ = "bingo_user" user_id = mapped_column(Integer, primary_key=True, nullable=False) username = mapped_column(String(100), nullable=False) + password = mapped_column(String(100), nullable=False) created_at = mapped_column( DateTime(timezone=True), default=lambda: datetime.now(ZoneInfo("Asia/Seoul")), nullable=False ) @classmethod - async def create(cls, session: AsyncSession, username: str): + async def create(cls, session: AsyncSession, username: str, password: str): is_user = await session.execute(select(cls).where(cls.username == username)) is_user = is_user.one_or_none() if is_user: - raise ValueError(f"{username}은 이미 존재하는 유저입니다. 이름에 2를 붙여 가입해주세요.") - new_user = BingoUser(username=username) + raise ValueError(f"{username}은 이미 존재하는 유저입니다. 다른 이름을 사용해주세요.") + new_user = BingoUser(username=username, password=password) session.add(new_user) await session.commit() await session.refresh(new_user) @@ -32,8 +33,6 @@ async def create(cls, session: AsyncSession, username: str): async def get_user_by_name(cls, session: AsyncSession, username: str): res = await session.execute(select(cls).where(cls.username == username)) user = res.scalars().first() - if not user: - raise ValueError(f"{username} 의 빙고 유저가 존재하지 않습니다.") return user @classmethod