big update new dashboard and import export
This commit is contained in:
@@ -2,12 +2,15 @@ from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from fastapi.security import OAuth2PasswordRequestForm
|
||||
from pydantic import BaseModel, field_validator
|
||||
from typing import Optional
|
||||
from datetime import datetime, timezone
|
||||
import uuid
|
||||
|
||||
from bd import make_engine
|
||||
|
||||
from bd.tables.users import User
|
||||
from route.auth_utils import hash_password, verify_password, create_access_token
|
||||
from route.auth_utils import hash_password, verify_password, create_access_token, SECRET_KEY, ALGORITHM, oauth2_scheme
|
||||
from jose import jwt, JWTError
|
||||
from redis_db import blacklist_token
|
||||
|
||||
router = APIRouter(tags=["auth"], prefix="/auth")
|
||||
|
||||
@@ -75,3 +78,18 @@ def login(form: OAuth2PasswordRequestForm = Depends()):
|
||||
)
|
||||
token = create_access_token({"sub": str(user.id)})
|
||||
return {"access_token": token, "token_type": "bearer"}
|
||||
|
||||
|
||||
@router.post("/logout")
|
||||
def logout(token: str = Depends(oauth2_scheme)):
|
||||
"""Инвалидирует текущий Bearer-токен через Redis блэклист."""
|
||||
try:
|
||||
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
|
||||
jti: str | None = payload.get("jti")
|
||||
exp: int | None = payload.get("exp")
|
||||
if jti and exp:
|
||||
ttl = max(0, exp - int(datetime.now(timezone.utc).timestamp()))
|
||||
blacklist_token(jti, ttl)
|
||||
except JWTError:
|
||||
pass # токен уже невалиден — ничего не делаем
|
||||
return {"detail": "Successfully logged out"}
|
||||
|
||||
Reference in New Issue
Block a user