Initial Animatrix import
This commit is contained in:
30
backend/app/core/deps.py
Normal file
30
backend/app/core/deps.py
Normal file
@@ -0,0 +1,30 @@
|
||||
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
|
||||
Reference in New Issue
Block a user