Spaces:
Runtime error
Runtime error
Download auth/dependencies.py from MaheenGhouri/fullstack_todo_app: direct link, hf CLI and curl.
- Browser
- Download file 1.38 kB
-
https://huggingface.co/spaces/MaheenGhouri/fullstack_todo_app/resolve/main/auth/dependencies.py
- Command line
-
hf download hf://spaces/MaheenGhouri/fullstack_todo_app/auth/dependencies.py
-
curl -L -o dependencies.py https://huggingface.co/spaces/MaheenGhouri/fullstack_todo_app/resolve/main/auth/dependencies.py
1.38 kB
| from fastapi import Depends, HTTPException, status | |
| from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials | |
| from typing import Optional | |
| from auth.jwt import verify_token | |
| from models import User | |
| from sqlmodel import Session | |
| from db import get_session | |
| from utils.errors import InvalidTokenError, ValidationError | |
| import uuid | |
| security = HTTPBearer() | |
| def get_current_user( | |
| credentials: HTTPAuthorizationCredentials = Depends(security), | |
| session: Session = Depends(get_session) | |
| ): | |
| """Get current user from token with additional validation""" | |
| token = credentials.credentials | |
| try: | |
| payload = verify_token(token) | |
| except HTTPException: | |
| raise InvalidTokenError() | |
| user_id_str = payload.get("sub") | |
| if user_id_str is None: | |
| raise InvalidTokenError() | |
| # Convert string UUID to proper UUID object for SQLModel | |
| try: | |
| user_id = uuid.UUID(user_id_str) | |
| except ValueError: | |
| raise InvalidTokenError() | |
| user = session.get(User, user_id) | |
| if user is None: | |
| raise InvalidTokenError() | |
| # Additional validation: check if user is active | |
| # (Assuming there's an 'is_active' field on the User model) | |
| # For now, we'll just return the user since the current User model doesn't have an active field | |
| # In a real implementation, we would check user account status here | |
| return user |