aigenrec/luminabackend
0
1from fastapi import Depends, HTTPException, status2from fastapi.security import OAuth2PasswordBearer3from jose import jwt, JWTError4from config.settings import settings5from typing import Optional6 7oauth2_scheme = OAuth2PasswordBearer(tokenUrl="api/v1/auth/login")8 9async def get_current_user(token: str = Depends(oauth2_scheme)):10 credentials_exception = HTTPException(11 status_code=status.HTTP_401_UNAUTHORIZED,12 detail="Could not validate credentials",13 headers={"WWW-Authenticate": "Bearer"},14 )15 try:16 payload = jwt.decode(token, settings.SECRET_KEY, algorithms=[settings.ALGORITHM])17 user_id: str = payload.get("sub")18 if user_id is None:19 raise credentials_exception20 return {"id": user_id, "email": payload.get("email")}21 except JWTError:22 raise credentials_exception23 