diff --git a/app/dependencies.py b/app/dependencies.py index c34a1f7..20a9a91 100644 --- a/app/dependencies.py +++ b/app/dependencies.py @@ -6,6 +6,8 @@ from fastapi.security import OAuth2PasswordBearer from jose import JWTError, jwt from passlib.context import CryptContext +from .models import users + fake_users = [ # password foo @@ -34,7 +36,7 @@ def get_password_hash(password): def get_user(db, username: str): for user in db: if username == user.username: - return UserInDB(**user) + return users.UserInDB(**user) def authenticate_user(fake_db, username: str, password: str): user = get_user(fake_db, username) @@ -72,3 +74,10 @@ async def get_current_user(token: Annotated[str, Depends(oauth2_scheme)]): if user is None: raise credentials_exception return user + +async def get_current_active_user( + current_user: Annotated[users.User, Depends(get_current_user)] +): + if current_user.disabled: + raise HTTPException(status_code=400, detail="Inactive user") + return current_user \ No newline at end of file diff --git a/app/routers/users.py b/app/routers/users.py index 079de7b..c6a7e31 100644 --- a/app/routers/users.py +++ b/app/routers/users.py @@ -1,9 +1,15 @@ -from fastapi import APIRouter +from fastapi import APIRouter, Depends from ..models import users +from ..dependencies import get_current_active_user +from typing import Annotated router = APIRouter() @router.get("/users/", tags=["users"]) async def read_users(): - return [{"username":"toto"}] \ No newline at end of file + return [{"username":"toto"}] + +@router.get("/users/me", response_model=users.User) +async def read_users_me(current_user: Annotated[users.User, Depends(get_current_active_user)]): + return [{"item_id": "Foo", "owner": current_user.username}] \ No newline at end of file