Skip to content

Commit 38966ea

Browse files
committed
refactor: moves to using user id in jwt payload REFS #52
previous implementation of the jwt subject was using email based on examples where the users always login with email + passwords. the template allows for OTP based logins where by the user may not have an email for an extended period of time, this refactors to the subject being set to using the user.id
1 parent 308515b commit 38966ea

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

src/labs/routers/auth/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ async def login_for_auth_token(
4040
""" Attempt to authenticate a user and issue JWT token
4141
4242
"""
43-
user = await User.get_by_email(session, form_data.username)
43+
user = await User.get_by_email(
44+
session,
45+
form_data.username
46+
)
4447

4548
if user is None or not user.check_password(form_data.password):
4649
raise HTTPException(
@@ -50,7 +53,7 @@ async def login_for_auth_token(
5053
)
5154

5255
access_token = create_access_token(
53-
subject=user.email,
56+
subject=user.id,
5457
fresh=True
5558
)
5659

src/labs/routers/utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,17 @@ async def get_current_user(
3838
algorithms=[config.JWT_ALGORITHM]
3939
)
4040

41-
username: str = payload.get("sub")
41+
user_id: str = payload.get("sub")
4242

43-
if username is None:
43+
if user_id is None:
4444
raise credentials_exception
4545

46-
token_data = TokenData(username=username)
46+
token_data = TokenData(id=user_id)
4747

4848
except:
4949
raise credentials_exception
5050

51-
user = await User.get_by_email(session, token_data.username)
51+
user = await User.get(session, token_data.id)
5252

5353
if user is None:
5454
raise credentials_exception

src/labs/schema/auth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class TokenData(BaseModel):
1616
is a valid token.
1717
1818
"""
19-
username: str = None
19+
id: str = None
2020

2121

2222
class SignupRequest(AppBaseModel):

0 commit comments

Comments
 (0)