31 lines
821 B
Python
31 lines
821 B
Python
from typing import Optional
|
|
|
|
from fastapi import Cookie, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.core.security import decode_access_token
|
|
from app.db.session import get_db
|
|
from app.models import User
|
|
|
|
|
|
def get_current_user(
|
|
access_token: Optional[str] = Cookie(default=None),
|
|
db: Session = Depends(get_db),
|
|
) -> User:
|
|
credentials_exception = HTTPException(
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
detail="Not authenticated",
|
|
)
|
|
if not access_token:
|
|
raise credentials_exception
|
|
|
|
user_id = decode_access_token(access_token)
|
|
if not user_id:
|
|
raise credentials_exception
|
|
|
|
user = db.query(User).filter(User.id == user_id, User.is_active.is_(True)).first()
|
|
if not user:
|
|
raise credentials_exception
|
|
|
|
return user
|