Skip to content

Session manager will always use memory for admin session storage #37

@arab0v

Description

@arab0v

I have tried to make the package use redis and postgres and in both cases it was uses memory instead, after tracing this issue i found that session_manager is initialized and never changes after using session_manager.

minimal code to reproduce

from contextlib import asynccontextmanager
from typing import AsyncGenerator

from fastapi import FastAPI
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
from sqlalchemy.orm import configure_mappers

DATABASE_URL = "postgresql+asyncpg://postgres:postgres@localhost:5432/testdb"
engine = create_async_engine(DATABASE_URL)

async_engine = create_async_engine(DATABASE_URL, echo=False, future=True)
local_session = async_sessionmaker(
    bind=async_engine, class_=AsyncSession, expire_on_commit=False
)


from crudadmin import CRUDAdmin

app = FastAPI()


# Create database session dependency
async def get_session() -> AsyncGenerator[AsyncSession, None]:
    async with local_session() as session:
        yield session


# # Create admin interface
admin = CRUDAdmin(
    session=get_session,
    SECRET_KEY="your-secret-key-here",
    initial_admin={"username": "admin", "password": "secure_password123"},
    secure_cookies=False,
).use_redis_sessions(redis_url="redis://localhost:6379/0")

admin_storage = admin.session_manager.storage

print("Admin storage initialized:", admin_storage)



@asynccontextmanager
async def lifespan(app: FastAPI):
    await admin.initialize()
    yield


# Create and mount the app
app = FastAPI(lifespan=lifespan)
app.mount("/admin", admin.app)


if __name__ == "__main__":
    import uvicorn

    uvicorn.run(app, host="0.0.0.0", port=8000)

here the output should be RedisSessionStorage instead of MemorySessionStorage
Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions