Spaces:
Paused
Paused
File size: 1,663 Bytes
1b7f275 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | from typing import AsyncGenerator
from fastapi import Depends, HTTPException, status
from fastapi.security import OAuth2PasswordBearer
from jose import jwt, JWTError
from pydantic import ValidationError
from sqlalchemy.ext.asyncio import AsyncSession
from app.core.config import settings
from app.db.session import SessionLocal
from app.models.user import User
from app.schemas.token import TokenPayload
from app.crud import user as user_crud
reusable_oauth2 = OAuth2PasswordBearer(tokenUrl=f"/api/v1/auth/login")
async def get_db() -> AsyncGenerator:
async with SessionLocal() as db:
try:
yield db
finally:
# SessionLocal จะปิดตัวเองอัตโนมัติเมื่อจบ async with
# หรือถ้าไม่ได้ใช้ context manager ต้อง await db.close() เอง
pass
async def get_current_user(
db: AsyncSession = Depends(get_db), token: str = Depends(reusable_oauth2)
) -> User:
try:
payload = jwt.decode(
token, settings.SECRET_KEY, algorithms=[settings.ALGORITHM]
)
token_data = TokenPayload(**payload)
except (JWTError, ValidationError):
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Could not validate credentials",
)
user = await user_crud.get_user_by_id(db, user_id=int(token_data.sub))
if not user:
raise HTTPException(status_code=404, detail="User not found")
return user
def get_current_active_user(
current_user: User = Depends(get_current_user),
) -> User:
return current_user
|