fix users

This commit is contained in:
Valentin CZERYBA 2023-10-12 00:14:50 +02:00
parent 4d38ecc08c
commit 6efe9fb5a7
2 changed files with 18 additions and 3 deletions

View File

@ -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

View File

@ -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"}]
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}]