Skip to content

Commit

Permalink
разбит логик модуль users.py
Browse files Browse the repository at this point in the history
  • Loading branch information
ProFastCode committed Jul 20, 2024
1 parent 7473d2e commit a5e3288
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/api/v1/users/auth/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ async def token(data: UserCreate, logic: deps.Logic):
"""
Retrieve new access token
"""
return await logic.users.generate_token(**data.model_dump())
return await logic.users.auth.generate_token(**data.model_dump())


__all__ = ["router"]
3 changes: 3 additions & 0 deletions app/logic/users/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .users import Users

__all__ = ["Users"]
3 changes: 3 additions & 0 deletions app/logic/users/auth/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .auth import Auth

__all__ = ["Auth"]
21 changes: 21 additions & 0 deletions app/logic/users/auth/auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from typing import TYPE_CHECKING

from app.core import exps
from app.models.token import AccessToken


if TYPE_CHECKING:
from app.logic import Logic


class Auth:
def __init__(self, logic: "Logic"):
self.logic = logic

async def generate_token(self, email: str, password: str) -> AccessToken | None:
if user := await self.logic.db.user.retrieve_by_email(email):
if not self.logic.security.pwd.checkpwd(password, user.password):
raise exps.UserIsCorrectException()
access_token = self.logic.security.jwt.encode_token({"id": user.id}, 1440)
return AccessToken(token=access_token)
raise exps.UserNotFoundException()
11 changes: 2 additions & 9 deletions app/logic/users.py → app/logic/users/users.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from typing import TYPE_CHECKING

from app.core import exps
from app.models.token import AccessToken
from app.models.user import User

from .auth import Auth

if TYPE_CHECKING:
from app.logic import Logic
Expand All @@ -12,6 +12,7 @@
class Users:
def __init__(self, logic: "Logic"):
self.logic = logic
self.auth = Auth(self.logic)

async def create(self, email: str, password: str) -> User | None:
if await self.logic.db.user.retrieve_by_email(email):
Expand All @@ -22,14 +23,6 @@ async def create(self, email: str, password: str) -> User | None:
user = await self.logic.db.user.create(model)
return user

async def generate_token(self, email: str, password: str) -> AccessToken | None:
if user := await self.logic.db.user.retrieve_by_email(email):
if not self.logic.security.pwd.checkpwd(password, user.password):
raise exps.UserIsCorrectException()
access_token = self.logic.security.jwt.encode_token({"id": user.id}, 1440)
return AccessToken(token=access_token)
raise exps.UserNotFoundException()

async def retrieve_by_token(self, token: str) -> User | None:
if payload := self.logic.security.jwt.decode_token(token):
if not (
Expand Down

0 comments on commit a5e3288

Please sign in to comment.