fixed token problem

This commit is contained in:
bdrtr 2025-05-05 14:31:25 +03:00
parent 5f32db24bf
commit 7281477269
3 changed files with 56 additions and 11 deletions

View file

@ -42,6 +42,12 @@ class User(BaseModel):
class UserInDb(User):
hashed_password : str | None = None
class UserPublic(BaseModel):
username : str | None = None
role : Role | None = None
status : Status | None = None
user_id : int | None = None
fake_db = {
"bedir": {
@ -75,7 +81,6 @@ def get_password_hash(password: str) -> str:
return pwd_context.hash(password)
def authenticate_user(fake_db, username: str, password: str) -> UserInDb | bool:
print("username", username)
user = fake_db.get(username)
if not user:
return False
@ -101,7 +106,7 @@ def get_user(db, username: str) -> UserInDb | None:
return UserInDb(**user_dict)
return None
def get_current_user(token: Annotated[str, Depends(oauth2_scheme)]) -> UserInDb | None:
def get_current_user(token: Annotated[str, Depends(oauth2_scheme)]) -> UserPublic | None:
credentials_exception = HTTPException(
status_code=401,
detail="Burda bir hata var",
@ -110,6 +115,7 @@ def get_current_user(token: Annotated[str, Depends(oauth2_scheme)]) -> UserInDb
try:
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
token_data = TokenData(**payload)
token_data.username = payload.get("sub")
username : str = token_data.username
if username is None:
raise credentials_exception
@ -125,11 +131,10 @@ def get_current_user(token: Annotated[str, Depends(oauth2_scheme)]) -> UserInDb
async def get_current_active_user(
current_user : Annotated[UserInDb, Depends(get_current_user)]
) -> UserInDb | None:
) -> UserPublic | None:
if current_user.status == Status.banned:
raise HTTPException(status_code=400, detail="Inactive user")
print("current_user", current_user)
return current_user
"""