51 lines
1.6 KiB
Python
Executable File
51 lines
1.6 KiB
Python
Executable File
from typing import AsyncGenerator
|
|
from fastapi import Depends, HTTPException, status
|
|
from fastapi.security import OAuth2PasswordBearer
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from sqlalchemy import select
|
|
|
|
from app.db.session import get_db
|
|
from app.core.security import decode_token
|
|
from app.models.identity import User # Javítva identity-re
|
|
|
|
# Javítva v1-re
|
|
reusable_oauth2 = OAuth2PasswordBearer(tokenUrl="/api/v1/auth/login")
|
|
|
|
async def get_current_user(
|
|
db: AsyncSession = Depends(get_db),
|
|
token: str = Depends(reusable_oauth2),
|
|
) -> User:
|
|
payload = decode_token(token)
|
|
if not payload:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
detail="Érvénytelen vagy lejárt token."
|
|
)
|
|
|
|
user_id = payload.get("sub")
|
|
if not user_id:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
detail="Token azonosítási hiba."
|
|
)
|
|
|
|
# Felhasználó keresése az adatbázisban
|
|
res = await db.execute(select(User).where(User.id == int(user_id)))
|
|
user = res.scalar_one_or_none()
|
|
|
|
if not user:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Felhasználó nem található."
|
|
)
|
|
|
|
if user.is_deleted:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
detail="Ez a fiók törölve lett."
|
|
)
|
|
|
|
# FONTOS: Itt NEM dobunk hibát, ha user.is_active == False,
|
|
# mert a Step 2 (KYC) kitöltéséhez be kell tudnia lépni inaktívként is!
|
|
|
|
return user |