21 lines
719 B
Python
21 lines
719 B
Python
from fastapi import Depends, HTTPException
|
|
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
|
from jose import JWTError
|
|
|
|
from app.auth.service import verify_token
|
|
from app.user.model import User
|
|
from app.user.service import get_user_by_id
|
|
from app.db import get_db
|
|
|
|
|
|
def get_auth_user(auth: HTTPAuthorizationCredentials = Depends(HTTPBearer()), db=Depends(get_db)) -> User:
|
|
try:
|
|
decoded_token = verify_token(auth.credentials)
|
|
auth_user = get_user_by_id(db, int(decoded_token["sub"]))
|
|
|
|
if auth_user is None:
|
|
raise HTTPException(status_code=401)
|
|
|
|
return auth_user
|
|
except JWTError:
|
|
raise HTTPException(status_code=401, detail="Invalid token")
|