Skip to content

Commit

Permalink
빙고 로그인 패스워드 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
4roring committed Nov 21, 2024
1 parent aeb35f8 commit 5c5a1af
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
6 changes: 3 additions & 3 deletions app/api/auth/routes.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down
10 changes: 7 additions & 3 deletions app/api/auth/services/bingo_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
9 changes: 4 additions & 5 deletions app/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 5c5a1af

Please sign in to comment.