create cur user
This commit is contained in:
30
src/api/dependacies/user_dep.py
Normal file
30
src/api/dependacies/user_dep.py
Normal file
@@ -0,0 +1,30 @@
|
||||
from typing import Annotated
|
||||
|
||||
from fastapi import HTTPException, Depends
|
||||
from fastapi.security import OAuth2PasswordBearer
|
||||
from jwt import InvalidTokenError
|
||||
|
||||
from src.core.auth_manager import AuthManager
|
||||
from src.core.settings import settings
|
||||
from src.schemas.auth import TokenData
|
||||
|
||||
oauth2_scheme = OAuth2PasswordBearer(tokenUrl=f"{settings.api.v1_login_url}/login")
|
||||
|
||||
|
||||
async def get_current_user(token: Annotated[str, Depends(oauth2_scheme)]):
|
||||
credentials_exception = HTTPException(
|
||||
status_code=401,
|
||||
detail="Could not validate credentials",
|
||||
headers={"WWW-Authenticate": "Bearer"},
|
||||
)
|
||||
try:
|
||||
payload = AuthManager.decode_access_token(token=token)
|
||||
if payload is None:
|
||||
raise credentials_exception
|
||||
user = TokenData(**payload)
|
||||
except InvalidTokenError:
|
||||
raise credentials_exception
|
||||
return user
|
||||
|
||||
|
||||
CurUsr = Annotated[TokenData, Depends(get_current_user)]
|
||||
@@ -1,5 +1,11 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
from src.api.dependacies.user_dep import CurUsr
|
||||
from src.core.settings import settings
|
||||
|
||||
router = APIRouter(prefix=settings.api.v1.users, tags=["Users"])
|
||||
|
||||
|
||||
@router.get("/me")
|
||||
async def get_me(user: CurUsr):
|
||||
return {"user"}
|
||||
|
||||
Reference in New Issue
Block a user