move users_active to folder dependancies

This commit is contained in:
Valentin CZERYBA 2023-10-12 23:35:45 +02:00
parent e8d2670024
commit f5da8ed3c4
4 changed files with 11 additions and 11 deletions

View File

View File

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

View File

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

View File

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