Spaces:
Sleeping
Sleeping
Download app/deps.py from Taimwe/trainhub-api: direct link, hf CLI and curl.
- Browser
- Download file 926 Bytes
-
https://huggingface.co/spaces/Taimwe/trainhub-api/resolve/main/app/deps.py
- Command line
-
hf download hf://spaces/Taimwe/trainhub-api/app/deps.py
-
curl -L -o deps.py https://huggingface.co/spaces/Taimwe/trainhub-api/resolve/main/app/deps.py
926 Bytes
| """FastAPI dependencies: DB session and current-user resolution.""" | |
| from fastapi import Depends, HTTPException, status | |
| from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials | |
| from sqlalchemy.orm import Session | |
| from . import models, security | |
| from .database import get_db | |
| bearer_scheme = HTTPBearer(auto_error=False) | |
| def get_current_user( | |
| cred: HTTPAuthorizationCredentials | None = Depends(bearer_scheme), | |
| db: Session = Depends(get_db), | |
| ) -> models.User: | |
| if cred is None: | |
| raise HTTPException(status.HTTP_401_UNAUTHORIZED, "Not authenticated") | |
| try: | |
| payload = security.decode_access_token(cred.credentials) | |
| except Exception: | |
| raise HTTPException(status.HTTP_401_UNAUTHORIZED, "Invalid or expired token") | |
| user = db.get(models.User, payload.get("sub")) | |
| if user is None: | |
| raise HTTPException(status.HTTP_401_UNAUTHORIZED, "User not found") | |
| return user | |