mp_driver_server/core/helpers/profile_helpers.py

22 lines
735 B
Python
Raw Normal View History

2024-06-26 16:44:12 +03:00
import jwt
from fastapi import Depends, HTTPException
from fastapi.security import OAuth2PasswordBearer
from core.config import Config
from core.model.profile.db import ProfileDB
from core.storage import profile
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/api/v1/auth/phone")
def get_user_from_token(token: str = Depends(oauth2_scheme)) -> ProfileDB:
try:
data = jwt.decode(token, algorithms="HS256", key=Config.secret)
except jwt.exceptions.ExpiredSignatureError as err:
raise HTTPException(status_code=401, detail=str(err))
except jwt.exceptions.InvalidSignatureError as err:
raise HTTPException(status_code=401, detail=str(err))
return profile.get_profile_by_id(data['profile_id'])