fix: small fixes to accompany the docs implementation (#35)

This commit is contained in:
Jayden Pyles
2024-11-07 12:24:59 -06:00
committed by GitHub
parent 86a1d32990
commit 7dfa3ccfe9
5 changed files with 43 additions and 26 deletions

View File

@@ -4,6 +4,7 @@ from gc import disable
from queue import Empty
from typing import Any, Optional
from datetime import datetime, timedelta
import logging
# PDM
from jose import JWTError, jwt
@@ -16,6 +17,8 @@ from fastapi.security import OAuth2PasswordBearer
from api.backend.schemas import User, UserInDB, TokenData
from api.backend.database import get_user_collection
LOG = logging.getLogger(__name__)
_ = load_dotenv()
SECRET_KEY = os.getenv("SECRET_KEY") or ""
@@ -74,10 +77,16 @@ def create_access_token(
async def get_current_user(token: str = Depends(oauth2_scheme)):
LOG.info(f"Getting current user with token: {token}")
if not token:
return EMPTY_USER
try:
payload: Optional[dict[str, Any]] = jwt.decode(
token, SECRET_KEY, algorithms=[ALGORITHM]
)
if not payload:
return EMPTY_USER
@@ -91,6 +100,10 @@ async def get_current_user(token: str = Depends(oauth2_scheme)):
except JWTError:
return EMPTY_USER
except Exception as e:
LOG.error(f"Exception occurred: {e}")
return EMPTY_USER
user = await get_user(email=token_data.email)
if user is None: