Skip to content

Commit 4341c7c

Browse files
authored
fix type annotations for TokenAuth (#310)
1 parent 77ea4be commit 4341c7c

File tree

2 files changed

+7
-16
lines changed

2 files changed

+7
-16
lines changed

piccolo_api/token_auth/endpoints.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ async def get_token(self, username: str, password: str) -> t.Optional[str]:
3333
if user:
3434
response = (
3535
await TokenAuth.select(TokenAuth.token)
36-
.first()
3736
.where(TokenAuth.user == user)
38-
.run()
37+
.first()
3938
)
40-
return response["token"]
39+
if response:
40+
return response["token"]
4141

4242
return None
4343

piccolo_api/token_auth/tables.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
from piccolo.table import Table
99
from piccolo.utils.sync import run_sync
1010

11-
if t.TYPE_CHECKING: # pragma: no cover
12-
from piccolo.query.methods.select import First
13-
1411

1512
def generate_token() -> str:
1613
return str(uuid.uuid4())
@@ -56,23 +53,17 @@ def create_token_sync(cls, user_id: int) -> str:
5653
return run_sync(cls.create_token(user_id))
5754

5855
@classmethod
59-
async def authenticate(cls, token: str) -> First:
60-
return cls.select(cls.user.id).where(cls.token == token).first()
56+
async def authenticate(cls, token: str) -> t.Optional[t.Dict]:
57+
return await cls.select(cls.user).where(cls.token == token).first()
6158

6259
@classmethod
63-
async def authenticate_sync(cls, token: str) -> First:
60+
def authenticate_sync(cls, token: str) -> t.Optional[t.Dict]:
6461
return run_sync(cls.authenticate(token))
6562

6663
@classmethod
6764
async def get_user_id(cls, token: str) -> t.Optional[int]:
6865
"""
6966
Returns the user_id if the given token is valid, otherwise None.
7067
"""
71-
data = (
72-
await cls.select(cls.user)
73-
.where(cls.token == token)
74-
.output(as_list=True)
75-
.first()
76-
.run()
77-
)
68+
data = await cls.authenticate(token=token)
7869
return data.get("user", None) if data else None

0 commit comments

Comments
 (0)