add user id in jwt token
parent
35bb1c5fb5
commit
15549f53d5
|
@ -1,3 +1,5 @@
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
from . import ReadDTO, WriteDTO
|
from . import ReadDTO, WriteDTO
|
||||||
|
|
||||||
|
|
||||||
|
@ -7,7 +9,7 @@ class UserDTO(WriteDTO):
|
||||||
|
|
||||||
|
|
||||||
class UserAuthDTO(UserDTO):
|
class UserAuthDTO(UserDTO):
|
||||||
id: str
|
id: UUID
|
||||||
|
|
||||||
|
|
||||||
class UserWriteDTO(UserDTO):
|
class UserWriteDTO(UserDTO):
|
||||||
|
|
|
@ -22,7 +22,7 @@ async def get_current_user(token: str = Depends(oauth2_schema)):
|
||||||
try:
|
try:
|
||||||
payload = jwt.decode(token, "fsgddfsgdfgs", algorithms=["HS256"])
|
payload = jwt.decode(token, "fsgddfsgdfgs", algorithms=["HS256"])
|
||||||
|
|
||||||
id: str = payload.get("id", "")
|
id: UUID = UUID(payload.get("id"))
|
||||||
name: str = payload.get("name", "")
|
name: str = payload.get("name", "")
|
||||||
sub: str = payload.get("sub", "")
|
sub: str = payload.get("sub", "")
|
||||||
expires_at: str = payload.get("expires_at", "")
|
expires_at: str = payload.get("expires_at", "")
|
||||||
|
@ -33,7 +33,6 @@ async def get_current_user(token: str = Depends(oauth2_schema)):
|
||||||
if expires_at:
|
if expires_at:
|
||||||
if is_expired(expires_at):
|
if is_expired(expires_at):
|
||||||
raise HTTPException(401, "Invalid credentials")
|
raise HTTPException(401, "Invalid credentials")
|
||||||
|
|
||||||
return UserAuthDTO(id=id, name=name, email=sub)
|
return UserAuthDTO(id=id, name=name, email=sub)
|
||||||
except JWTError:
|
except JWTError:
|
||||||
raise HTTPException(401, "Invalid credentials")
|
raise HTTPException(401, "Invalid credentials")
|
||||||
|
@ -50,7 +49,9 @@ class AuthService:
|
||||||
self.uow = uow
|
self.uow = uow
|
||||||
self.crypto_context = CryptContext(schemes="bcrypt")
|
self.crypto_context = CryptContext(schemes="bcrypt")
|
||||||
|
|
||||||
async def authenticate(self, login: OAuth2PasswordRequestForm = Depends()) -> TokenSchema | None:
|
async def authenticate(
|
||||||
|
self, login: OAuth2PasswordRequestForm = Depends()
|
||||||
|
) -> TokenSchema | None:
|
||||||
async with self.uow:
|
async with self.uow:
|
||||||
user = await self.uow.users.find_one(filter={"email": login.username})
|
user = await self.uow.users.find_one(filter={"email": login.username})
|
||||||
|
|
||||||
|
@ -73,6 +74,7 @@ class AuthService:
|
||||||
"expires_at": self._expiration_time(),
|
"expires_at": self._expiration_time(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
print(payload)
|
||||||
return jwt.encode(payload, "fsgddfsgdfgs", algorithm="HS256")
|
return jwt.encode(payload, "fsgddfsgdfgs", algorithm="HS256")
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|
Loading…
Reference in New Issue