Spaces:
Sleeping
Sleeping
Download src/api/dependencies.py from bhaimavi353/todo-project: direct link, hf CLI and curl.
- Browser
- Download file 1.08 kB
-
https://huggingface.co/spaces/bhaimavi353/todo-project/resolve/main/src/api/dependencies.py
- Command line
-
hf download hf://spaces/bhaimavi353/todo-project/src/api/dependencies.py
-
curl -L -o dependencies.py https://huggingface.co/spaces/bhaimavi353/todo-project/resolve/main/src/api/dependencies.py
1.08 kB
| from typing import Annotated | |
| from fastapi import Depends, HTTPException, status | |
| from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials | |
| from sqlmodel import Session | |
| from src.database import get_session | |
| from src.services import AuthService | |
| from src.models import User | |
| security = HTTPBearer() | |
| async def get_current_user( | |
| credentials: Annotated[HTTPAuthorizationCredentials, Depends(security)], | |
| session: Annotated[Session, Depends(get_session)] | |
| ) -> User: | |
| token = credentials.credentials | |
| payload = AuthService.decode_token(token) | |
| if not payload: | |
| raise HTTPException( | |
| status_code=status.HTTP_401_UNAUTHORIZED, | |
| detail="Invalid or expired token", | |
| headers={"WWW-Authenticate": "Bearer"} | |
| ) | |
| user_id = int(payload.get("sub")) | |
| user = AuthService.get_user_by_id(session, user_id) | |
| if not user: | |
| raise HTTPException( | |
| status_code=status.HTTP_401_UNAUTHORIZED, | |
| detail="User not found", | |
| headers={"WWW-Authenticate": "Bearer"} | |
| ) | |
| return user | |