Spaces:
Paused
Paused
| 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 | |