File size: 1,087 Bytes
358dfff | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | from fastapi import Depends, HTTPException, status
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from typing import Optional
from models.user import User
from utils.jwt_utils import is_authenticated
# HTTP Bearer token scheme for authentication
security = HTTPBearer()
def get_current_user(credentials: HTTPAuthorizationCredentials = Depends(security)) -> Optional[User]:
"""
Dependency to get the current authenticated user from the JWT token
"""
token = credentials.credentials
user = is_authenticated(token)
if user is None:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Could not validate credentials",
headers={"WWW-Authenticate": "Bearer"},
)
return user
def get_optional_user(credentials: HTTPAuthorizationCredentials = Depends(security)) -> Optional[User]:
"""
Dependency to get the current user if authenticated, or return None
"""
token = credentials.credentials
user = is_authenticated(token)
return user |