Created
December 28, 2022 09:30
-
-
Save alksndrglk/de040fd7e247cfb71ad30fba3069f495 to your computer and use it in GitHub Desktop.
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
| # --- queries.py ---- | |
| async def get_by_email(self, email: str) -> typing.Union[Admin, None]: | |
| async with self.app.database.session() as session: | |
| admin = await session.execute( | |
| select(AdminModel).where(AdminModel.email == email) | |
| ) | |
| if admin: | |
| for admin in admin.scalars(): | |
| return Admin( | |
| id=admin.id, email=admin.email, password=admin.password | |
| ) | |
| return None | |
| # --- models.py ---- | |
| @dataclass | |
| class Admin: | |
| id: int | |
| email: str | |
| password: Optional[str] = None | |
| def is_password_valid(self, password: str): | |
| return self.password == sha256(password.encode()).hexdigest() | |
| class AdminModel(db): | |
| __tablename__ = "admins" | |
| id = Column(Integer, primary_key=True) | |
| email = Column(String, unique=True, nullable=False) | |
| password = Column(String, nullable=False) | |
| # --- view.py --- | |
| class AdminLoginView(View): | |
| async def post(self): | |
| email, password = self.data["email"], self.data["password"] | |
| admin = await self.queries.get_by_email(email) | |
| if not admin or not admin.is_password_valid(password): | |
| raise HTTPForbidden |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment