Spaces:
Sleeping
Sleeping
| from database import SessionLocal | |
| from fastapi import Depends, HTTPException | |
| from sqlalchemy.orm import Session | |
| from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials | |
| from auth import decode_token | |
| security = HTTPBearer() | |
| def get_db(): | |
| db = SessionLocal() | |
| try: | |
| yield db | |
| finally: | |
| db.close() | |
| def get_current_user( | |
| creds: HTTPAuthorizationCredentials = Depends(security) | |
| ): | |
| try: | |
| payload = decode_token(creds.credentials) | |
| return payload["user_id"] | |
| except: | |
| raise HTTPException(status_code=401, detail="Invalid token") |