diff --git a/app/dependencies/__init__.py b/app/dependencies/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/dependencies.py b/app/dependencies/users_active.py similarity index 98% rename from app/dependencies.py rename to app/dependencies/users_active.py index 5692bb6..9ca2b78 100644 --- a/app/dependencies.py +++ b/app/dependencies/users_active.py @@ -6,7 +6,7 @@ from fastapi.security import OAuth2PasswordBearer from jose import JWTError, jwt from passlib.context import CryptContext -from .models import users, token +from ..models import users, token fake_users = [ diff --git a/app/routers/token.py b/app/routers/token.py index 9a889d9..4d99f28 100644 --- a/app/routers/token.py +++ b/app/routers/token.py @@ -3,7 +3,7 @@ from datetime import datetime, timedelta from typing import Annotated from fastapi import Depends, FastAPI, HTTPException, status, APIRouter from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm -from ..dependencies import fake_users, authenticate_user, ACCESS_TOKEN_EXPIRE_MINUTES, create_access_token +from ..dependencies import users_active from ..models import token router = APIRouter() @@ -13,15 +13,15 @@ router = APIRouter() async def login_for_access_token( form_data: Annotated[OAuth2PasswordRequestForm, Depends()] ): - user = authenticate_user(fake_users, form_data.username, form_data.password) + user = users_active.authenticate_user(users_active.fake_users, form_data.username, form_data.password) if not user: raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Incorrect username or password", headers={"WWW-Authenticate": "Bearer"}, ) - access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES) - access_token = create_access_token( + access_token_expires = timedelta(minutes=users_active.ACCESS_TOKEN_EXPIRE_MINUTES) + access_token = users_active.create_access_token( data={"sub": user.username}, expires_delta=access_token_expires ) return {"access_token": access_token, "token_type": "bearer"} \ No newline at end of file diff --git a/app/routers/users.py b/app/routers/users.py index 3dc18db..250013b 100644 --- a/app/routers/users.py +++ b/app/routers/users.py @@ -1,15 +1,15 @@ from fastapi import APIRouter, Depends +from ..dependencies import users_active 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"}] +@router.get("/users/", tags=["users"], response_model=list[users.User]) +async def read_users(current_user: Annotated[users.User, Depends(users_active.get_current_active_user)]): + return users_active.fake_users -@router.get("/users/me", response_model=users.User) -async def read_users_me(current_user: Annotated[users.User, Depends(get_current_active_user)]): +@router.get("/users/me",tags=["users"], response_model=users.User) +async def read_users_me(current_user: Annotated[users.User, Depends(users_active.get_current_active_user)]): return current_user \ No newline at end of file