Session Config¶
Similar to the SessionConfig
from Esmerald, this one follows the same principle and for that reason we kept the same
name to make it easier and faster to understand.
The config class¶
The SessionConfig is used to pass the necessary parameters to the SessionMiddleware object with all the needed parameters. Like every single configuration of Esmerald, this also uses Pydantic and leverages the power of the library.
from esmerald import Esmerald
from esmerald.utils.crypto import get_random_secret_key
from redis import Redis
from starlette.middleware import Middleware
from esmerald_sessions import SessionConfig, SessionMiddleware
from esmerald_sessions.enums import BackendType
redis_client = Redis(host="localhost", port=6379)
session_config = SessionConfig(
secret_key=get_random_secret_key(),
cookie_name="cookie",
backend_type=BackendType.redis,
backend_client=redis_client,
)
app = Esmerald(
routes=[...],
middleware=[Middleware(SessionMiddleware, config=session_config)],
)
The way of passing the configurations is demonstrated in the usage section of the documentation with more examples how to do it.
Parameter¶
-
secret_key - The string used for the encryption/decryption. We advise to use the same secret as the one in the settings to make it consistent.
-
path - The path for the cookie.
Default:
/
-
cookie_name - The name for the session cookie.
-
max_age - The number in seconds until the cookie expires.
Default:
14 * 24 * 60 * 60
seconds -
https_only - Boolean if set enforces the session cookie to be httpsOnly.
Default:
False
-
same_site - Level of restriction for the session cookie. More on this.
Default:
lax
-
domain - The domain associated to the cookie.
Default:
None
-
backend_type - String type of predefined backend to use.
Default:
None
-
backend_client - The client to use in the predefined backend.
Default:
None
-
custom_session_backend - A custom backend that implement SessionBackend.
Default:
None