Skip to content

Commit a5e3288

Browse files
committed
разбит логик модуль users.py
1 parent 7473d2e commit a5e3288

File tree

5 files changed

+30
-10
lines changed

5 files changed

+30
-10
lines changed

app/api/v1/users/auth/token.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ async def token(data: UserCreate, logic: deps.Logic):
1212
"""
1313
Retrieve new access token
1414
"""
15-
return await logic.users.generate_token(**data.model_dump())
15+
return await logic.users.auth.generate_token(**data.model_dump())
1616

1717

1818
__all__ = ["router"]

app/logic/users/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .users import Users
2+
3+
__all__ = ["Users"]

app/logic/users/auth/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .auth import Auth
2+
3+
__all__ = ["Auth"]

app/logic/users/auth/auth.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from typing import TYPE_CHECKING
2+
3+
from app.core import exps
4+
from app.models.token import AccessToken
5+
6+
7+
if TYPE_CHECKING:
8+
from app.logic import Logic
9+
10+
11+
class Auth:
12+
def __init__(self, logic: "Logic"):
13+
self.logic = logic
14+
15+
async def generate_token(self, email: str, password: str) -> AccessToken | None:
16+
if user := await self.logic.db.user.retrieve_by_email(email):
17+
if not self.logic.security.pwd.checkpwd(password, user.password):
18+
raise exps.UserIsCorrectException()
19+
access_token = self.logic.security.jwt.encode_token({"id": user.id}, 1440)
20+
return AccessToken(token=access_token)
21+
raise exps.UserNotFoundException()

app/logic/users.py renamed to app/logic/users/users.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from typing import TYPE_CHECKING
22

33
from app.core import exps
4-
from app.models.token import AccessToken
54
from app.models.user import User
65

6+
from .auth import Auth
77

88
if TYPE_CHECKING:
99
from app.logic import Logic
@@ -12,6 +12,7 @@
1212
class Users:
1313
def __init__(self, logic: "Logic"):
1414
self.logic = logic
15+
self.auth = Auth(self.logic)
1516

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

25-
async def generate_token(self, email: str, password: str) -> AccessToken | None:
26-
if user := await self.logic.db.user.retrieve_by_email(email):
27-
if not self.logic.security.pwd.checkpwd(password, user.password):
28-
raise exps.UserIsCorrectException()
29-
access_token = self.logic.security.jwt.encode_token({"id": user.id}, 1440)
30-
return AccessToken(token=access_token)
31-
raise exps.UserNotFoundException()
32-
3326
async def retrieve_by_token(self, token: str) -> User | None:
3427
if payload := self.logic.security.jwt.decode_token(token):
3528
if not (

0 commit comments

Comments
 (0)