Изучаю FastAPI, при попытке авторизоваться ошибка в терминале:127.0.0.1:53926 - "POST /auth/jwt/logout HTTP/1.1" 204 No Content. При регистрации все ок, данные заносятся в таблицу.
main.py:
from fastapi import FastAPI, Depends
from fastapi_users import FastAPIUsers
from auth.auth import auth_backend
from auth.database import User
from auth.manager import get_user_manager
from auth.shemas import UserRead, UserCreate
app = FastAPI(
title="App"
)
fastapi_users = FastAPIUsers[User, int](
get_user_manager,
[auth_backend],
)
app.include_router(
fastapi_users.get_auth_router(auth_backend),
prefix="/auth/jwt",
tags=["auth"],
)
app.include_router(
fastapi_users.get_register_router(UserRead, UserCreate),
prefix="/auth",
tags=["auth"],
)
database.py:
from typing import AsyncGenerator
from fastapi import Depends
from fastapi_users.db import SQLAlchemyUserDatabase, SQLAlchemyBaseUserTable
from sqlalchemy import Column, Integer, String, Float, Date
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from sqlalchemy.ext.declarative import DeclarativeMeta, declarative_base
from sqlalchemy.orm import sessionmaker
DATABASE_URL = 'sqlite+aiosqlite:///./user.db'
Base: DeclarativeMeta = declarative_base() #похоже на metadata
class User(SQLAlchemyBaseUserTable[int], Base): #модель таблицы
id = Column(Integer, primary_key=True)
salary = Column(Float, nullable=False)
date = Column(String, nullable=False)
engine = create_async_engine(DATABASE_URL)
async_session_maker = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
async def get_async_session() -> AsyncGenerator[AsyncSession, None]:#получение асинхронной сессии
async with async_session_maker() as session:
yield session
async def get_user_db(session: AsyncSession = Depends(get_async_session)):#получение пользователя
yield SQLAlchemyUserDatabase(session, User)
auth.py:
from fastapi_users.authentication import CookieTransport, AuthenticationBackend
from fastapi_users.authentication import JWTStrategy
cookie_transport = CookieTransport(cookie_name="bonds",cookie_max_age=3600)
SECRET = "SECRET"
def get_jwt_strategy() -> JWTStrategy:
return JWTStrategy(secret=SECRET, lifetime_seconds=3600)
auth_backend = AuthenticationBackend(
name="jwt",
transport=cookie_transport,
get_strategy=get_jwt_strategy,
)