Spaces:
Running
Running
| from datetime import datetime, timedelta | |
| import jwt | |
| from fastapi import HTTPException, Depends | |
| from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials | |
| from .config import settings | |
| security_scheme = HTTPBearer() | |
| def create_jwt_token(user_id: str, email: str) -> str: | |
| """Creates a signed JWT with 7-day expiration.""" | |
| expiration = datetime.utcnow() + timedelta(days=7) | |
| payload = { | |
| "sub": user_id, | |
| "email": email, | |
| "exp": expiration | |
| } | |
| return jwt.encode(payload, settings.JWT_SECRET, algorithm=settings.ALGORITHM) | |
| def verify_jwt(credentials: HTTPAuthorizationCredentials = Depends(security_scheme)): | |
| """Dependency to verify JWT header.""" | |
| token = credentials.credentials | |
| try: | |
| payload = jwt.decode(token, settings.JWT_SECRET, algorithms=[settings.ALGORITHM]) | |
| return payload["sub"] # Returns user_id | |
| except jwt.ExpiredSignatureError: | |
| raise HTTPException(status_code=401, detail="Token expired") | |
| except jwt.InvalidTokenError: | |
| raise HTTPException(status_code=401, detail="Invalid token") | |