/
tsps
/
core
Обзор
Документация
Войти
/
tsps
/
core
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
master
app/api/lk/auth.py
74 строки
2 KB
Василий Петров
Аутентификация в ЛК с разделением с админкой
14 мар 2026, 14:37
14 мар 2026, 14:37
3d80156
Код
Авторство
О чём код?
from fastapi import APIRouter, HTTPException, status, Form, Depends from sqlalchemy.orm import Session, joinedload from app.database import get_db from app.models.judge_account import JudgeAccount from app.core.security import create_token, revoke_token, get_token_from_header router = APIRouter(tags=["lk", "auth"]) @router.post("/login") def login( email: str = Form(...), password: str = Form(...), db: Session = Depends(get_db) ): # Загружаем аккаунт вместе со связанным судьей account = ( db.query(JudgeAccount) .options(joinedload(JudgeAccount.judge)) .filter(JudgeAccount.email == email) .first() ) if not account: raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Неверный логин или пароль" ) if not account.is_active: raise HTTPException( status_code=status.HTTP_403_FORBIDDEN, detail="Аккаунт заблокирован" ) if not account.verify_password(password): raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Неверный логин или пароль" ) # Токен привязывается к ID учетной записи (account.id), а не к ID судьи token = create_token(account.id, owner_type="judge_account") # Формируем ответ с данными судьи judge_data = None if account.judge: judge_data = { "id": account.judge.id, "last_name": account.judge.last_name, "first_name": account.judge.first_name, "middle_name": account.judge.middle_name, } return { "access_token": token, "token_type": "bearer", "account": { "id": account.id, "email": account.email, }, "judge": judge_data } @router.post("/logout") def logout(token: str = Depends(get_token_from_header)): if revoke_token(token): return {"message": "Выход выполнен успешно"} else: raise HTTPException( status_code=401, detail="Токен недействителен или уже удалён" )