-
Notifications
You must be signed in to change notification settings - Fork 1
♻️ auth 패키지 구현체 및 DI 도입, email 패키지 분리 #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| from fastapi import Depends | ||
| from sqlalchemy.ext.asyncio import AsyncSession | ||
|
|
||
| from app.auth.repository import EmailVerificationRepository, UserRepository | ||
| from app.auth.repository_impl import EmailVerificationRepositoryImpl, UserRepositoryImpl | ||
| from app.auth.service import AuthService | ||
| from app.auth.service_impl import AuthServiceImpl | ||
| from app.db.session import get_db | ||
| from app.email.service import EmailSender | ||
| from app.email.service_impl import SmtpEmailSender | ||
|
|
||
|
|
||
| def get_user_repository(db: AsyncSession = Depends(get_db)) -> UserRepository: | ||
| return UserRepositoryImpl(db) | ||
|
|
||
|
|
||
| def get_email_verification_repository( | ||
| db: AsyncSession = Depends(get_db), | ||
| ) -> EmailVerificationRepository: | ||
| return EmailVerificationRepositoryImpl(db) | ||
|
|
||
|
|
||
| def get_email_sender() -> EmailSender: | ||
| return SmtpEmailSender() | ||
|
|
||
|
|
||
| def get_auth_service( | ||
| user_repo: UserRepository = Depends(get_user_repository), | ||
| ev_repo: EmailVerificationRepository = Depends(get_email_verification_repository), | ||
| email_sender: EmailSender = Depends(get_email_sender), | ||
| ) -> AuthService: | ||
| return AuthServiceImpl(user_repo, ev_repo, email_sender) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| from datetime import datetime | ||
| from typing import TYPE_CHECKING | ||
| from uuid import UUID, uuid4 | ||
|
|
||
| from sqlalchemy import Boolean, DateTime, Enum, String | ||
| from sqlalchemy.orm import Mapped, mapped_column, relationship | ||
| from sqlalchemy.sql import func | ||
|
|
||
| from app.db.base import Base | ||
|
|
||
| if TYPE_CHECKING: | ||
| from app.models.deposit import Deposit | ||
|
|
||
|
|
||
| class User(Base): | ||
| __tablename__ = "users" | ||
|
|
||
| id: Mapped[UUID] = mapped_column(primary_key=True, default=uuid4) | ||
| username: Mapped[str] = mapped_column(String(50)) | ||
| email: Mapped[str] = mapped_column(String(255), unique=True, index=True) | ||
| password_hash: Mapped[str] = mapped_column(String) | ||
| role: Mapped[str] = mapped_column(Enum("OWNER", "REVIEWER", name="user_role")) | ||
| created_at: Mapped[datetime] = mapped_column( | ||
| DateTime(timezone=True), server_default=func.now() | ||
| ) | ||
|
|
||
| deposits: Mapped[list["Deposit"]] = relationship("Deposit", back_populates="user") | ||
|
|
||
|
|
||
| class EmailVerification(Base): | ||
| __tablename__ = "email_verifications" | ||
|
|
||
| id: Mapped[UUID] = mapped_column(primary_key=True, default=uuid4) | ||
| email: Mapped[str] = mapped_column(String(255), index=True) | ||
| code: Mapped[str] = mapped_column(String(6)) | ||
| verification_token: Mapped[str | None] = mapped_column(String, nullable=True) | ||
| is_verified: Mapped[bool] = mapped_column(Boolean, default=False) | ||
| expires_at: Mapped[datetime] = mapped_column(DateTime(timezone=True)) | ||
| created_at: Mapped[datetime] = mapped_column( | ||
| DateTime(timezone=True), server_default=func.now() | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| from abc import ABC, abstractmethod | ||
|
|
||
| from app.auth.models import EmailVerification, User | ||
|
|
||
|
|
||
| class UserRepository(ABC): | ||
| @abstractmethod | ||
| async def find_by_email(self, email: str) -> User | None: ... | ||
|
|
||
| @abstractmethod | ||
| async def find_by_id(self, user_id: str) -> User | None: ... | ||
|
|
||
| @abstractmethod | ||
| async def save(self, user: User) -> User: ... | ||
|
|
||
|
|
||
| class EmailVerificationRepository(ABC): | ||
| @abstractmethod | ||
| async def save(self, verification: EmailVerification) -> None: ... | ||
|
|
||
| @abstractmethod | ||
| async def find_latest_unverified( | ||
| self, email: str, code: str | ||
| ) -> EmailVerification | None: ... | ||
|
|
||
| @abstractmethod | ||
| async def update(self, verification: EmailVerification) -> None: ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| from datetime import UTC, datetime | ||
| from typing import cast | ||
| from uuid import UUID | ||
|
|
||
| from sqlalchemy import select | ||
| from sqlalchemy.ext.asyncio import AsyncSession | ||
|
|
||
| from app.auth.models import EmailVerification, User | ||
| from app.auth.repository import EmailVerificationRepository, UserRepository | ||
|
|
||
|
|
||
| class UserRepositoryImpl(UserRepository): | ||
| def __init__(self, db: AsyncSession) -> None: | ||
| self.db = db | ||
|
|
||
| async def find_by_email(self, email: str) -> User | None: | ||
| return cast( | ||
| User | None, | ||
| await self.db.scalar(select(User).where(User.email == email)), | ||
| ) | ||
|
|
||
| async def find_by_id(self, user_id: str) -> User | None: | ||
| return cast( | ||
| User | None, | ||
| await self.db.scalar(select(User).where(User.id == UUID(user_id))), | ||
| ) | ||
|
|
||
| async def save(self, user: User) -> User: | ||
| self.db.add(user) | ||
| await self.db.commit() | ||
| await self.db.refresh(user) | ||
| return user | ||
|
|
||
|
|
||
| class EmailVerificationRepositoryImpl(EmailVerificationRepository): | ||
| def __init__(self, db: AsyncSession) -> None: | ||
| self.db = db | ||
|
|
||
| async def save(self, verification: EmailVerification) -> None: | ||
| self.db.add(verification) | ||
| await self.db.commit() | ||
|
|
||
| async def find_latest_unverified( | ||
| self, email: str, code: str | ||
| ) -> EmailVerification | None: | ||
| return cast( | ||
| EmailVerification | None, | ||
| await self.db.scalar( | ||
| select(EmailVerification) | ||
| .where( | ||
| EmailVerification.email == email, | ||
| EmailVerification.code == code, | ||
| EmailVerification.is_verified.is_(False), | ||
| EmailVerification.expires_at > datetime.now(UTC), | ||
| ) | ||
| .order_by(EmailVerification.created_at.desc()) | ||
| ), | ||
| ) | ||
|
|
||
| async def update(self, verification: EmailVerification) -> None: | ||
| await self.db.commit() | ||
|
Comment on lines
+35
to
+61
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 인증코드 저장 로직은 별도 repo로 분리 (User 생성하는 거랑 로직상으로 차이가 있음. 한방에 묶어서 관리하기보다는 별도로 분리했을 때 추후 디버깅할 때 여기만 보면 되니까 유리할 듯 |
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| from abc import ABC, abstractmethod | ||
|
|
||
| from app.auth.models import User | ||
| from app.auth.schemas import LoginReq, RegisterReq, ResetPasswordReq | ||
|
|
||
|
|
||
| class AuthService(ABC): | ||
| @abstractmethod | ||
| async def check_email_duplicate(self, email: str) -> None: ... | ||
|
|
||
| @abstractmethod | ||
| async def send_verification_code(self, email: str) -> None: ... | ||
|
|
||
| @abstractmethod | ||
| async def validate_verification_code(self, email: str, code: str) -> str: ... | ||
|
|
||
| @abstractmethod | ||
| async def register(self, data: RegisterReq) -> tuple[User, str, str]: ... | ||
|
|
||
| @abstractmethod | ||
| def refresh_access_token(self, refresh_token: str) -> str: ... | ||
|
|
||
| @abstractmethod | ||
| async def login(self, data: LoginReq) -> tuple[User, str, str]: ... | ||
|
|
||
| @abstractmethod | ||
| async def change_password( | ||
| self, user_id: str, old_password: str, new_password: str | ||
| ) -> None: ... | ||
|
|
||
| @abstractmethod | ||
| async def reset_password(self, data: ResetPasswordReq) -> None: ... |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
deposit 안 쓰니까 제거해야 됨
-> 별도 issue로 뽑아두기