mirror of
https://github.com/RYDE-WORK/full-stack-fastapi-template.git
synced 2026-01-19 21:23:36 +08:00
31 lines
1.0 KiB
Python
31 lines
1.0 KiB
Python
import jwt
|
|
from fastapi import Depends, HTTPException, Security
|
|
from fastapi.security import OAuth2PasswordBearer
|
|
from jwt import PyJWTError
|
|
from sqlalchemy.orm import Session
|
|
from starlette.status import HTTP_403_FORBIDDEN
|
|
|
|
from app.api.utils.db import get_db
|
|
from app.core import config
|
|
from app.core.jwt import ALGORITHM
|
|
from app.crud import user as crud_user
|
|
from app.models.token import TokenPayload
|
|
|
|
reusable_oauth2 = OAuth2PasswordBearer(tokenUrl="/api/v1/login/access-token")
|
|
|
|
|
|
def get_current_user(
|
|
db: Session = Depends(get_db), token: str = Security(reusable_oauth2)
|
|
):
|
|
try:
|
|
payload = jwt.decode(token, config.SECRET_KEY, algorithms=[ALGORITHM])
|
|
token_data = TokenPayload(**payload)
|
|
except PyJWTError:
|
|
raise HTTPException(
|
|
status_code=HTTP_403_FORBIDDEN, detail="Could not validate credentials"
|
|
)
|
|
user = crud_user.get(db, user_id=token_data.user_id)
|
|
if not user:
|
|
raise HTTPException(status_code=404, detail="User not found")
|
|
return user
|