19 lines
668 B
Python
19 lines
668 B
Python
from fastapi import Depends, HTTPException, status
|
|
from fastapi.security import OAuth2PasswordBearer
|
|
from typing import Optional
|
|
from schemas.auth import TokenData
|
|
from services.auth_service import verify_token
|
|
|
|
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="auth/login")
|
|
|
|
async def get_current_user(token: str = Depends(oauth2_scheme)) -> TokenData:
|
|
"""获取当前用户"""
|
|
token_data = verify_token(token)
|
|
if token_data is None:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
detail="Invalid authentication credentials",
|
|
headers={"WWW-Authenticate": "Bearer"},
|
|
)
|
|
return token_data
|