fix users
This commit is contained in:
parent
4d38ecc08c
commit
6efe9fb5a7
@ -6,6 +6,8 @@ from fastapi.security import OAuth2PasswordBearer
|
|||||||
from jose import JWTError, jwt
|
from jose import JWTError, jwt
|
||||||
from passlib.context import CryptContext
|
from passlib.context import CryptContext
|
||||||
|
|
||||||
|
from .models import users
|
||||||
|
|
||||||
|
|
||||||
fake_users = [
|
fake_users = [
|
||||||
# password foo
|
# password foo
|
||||||
@ -34,7 +36,7 @@ def get_password_hash(password):
|
|||||||
def get_user(db, username: str):
|
def get_user(db, username: str):
|
||||||
for user in db:
|
for user in db:
|
||||||
if username == user.username:
|
if username == user.username:
|
||||||
return UserInDB(**user)
|
return users.UserInDB(**user)
|
||||||
|
|
||||||
def authenticate_user(fake_db, username: str, password: str):
|
def authenticate_user(fake_db, username: str, password: str):
|
||||||
user = get_user(fake_db, username)
|
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:
|
if user is None:
|
||||||
raise credentials_exception
|
raise credentials_exception
|
||||||
return user
|
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
|
@ -1,5 +1,7 @@
|
|||||||
from fastapi import APIRouter
|
from fastapi import APIRouter, Depends
|
||||||
from ..models import users
|
from ..models import users
|
||||||
|
from ..dependencies import get_current_active_user
|
||||||
|
from typing import Annotated
|
||||||
|
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
@ -7,3 +9,7 @@ router = APIRouter()
|
|||||||
@router.get("/users/", tags=["users"])
|
@router.get("/users/", tags=["users"])
|
||||||
async def read_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}]
|
Loading…
x
Reference in New Issue
Block a user