Struct typed_session_axum::SessionLayer
source · pub struct SessionLayer<SessionData, SessionStoreConnection> { /* private fields */ }
Expand description
Layer that provides cookie-based sessions.
See SessionLayer::new
for more details.
Implementations§
source§impl<SessionData, SessionStoreConnection: SessionStoreConnector<SessionData>> SessionLayer<SessionData, SessionStoreConnection>
impl<SessionData, SessionStoreConnection: SessionStoreConnector<SessionData>> SessionLayer<SessionData, SessionStoreConnection>
sourcepub fn new() -> Self
pub fn new() -> Self
Creates a layer which will attach a SessionHandle
to requests via an
extension. This session is derived from a cookie. When the client sends
a valid, known cookie then the session is loaded using the cookie as key.
Otherwise, the SessionHandle
will contain a default session which is
only persisted if it was mutably accessed.
The layer expects the SessionStoreConnection
to exist as an extension.
It is a type that implements SessionStoreConnector
with the correct
SessionData
.
The type is required to implement Send
, Sync
and Clone
, such that
the session layer can make its own copy (required due to some details of
axum, specifically the extensions do not get automatically attached to a
response from the corresponding request).
The SessionStoreConnection
can e.g. be a type representing a simple
database connection, a connection pool or database transaction. Since
sessions cannot be updated concurrently, using transactions may be useful,
to be able to roll back all changes in case the session got updated.
It is important that the SessionStoreConnection
in the extension is
ready, because axum
does not use backpressure, but explicit readiness
checks.
Customization
The configuration of the session may be adjusted according to the needs of your application:
use typed_session::SessionRenewalStrategy;
SessionLayer::<i32, MemoryStore<i32, NoLogger>>::new()
.with_cookie_name("id") // for security reasons, just stick with the default "id" here
.with_cookie_path("/some/path")
.with_cookie_domain("www.example.com")
.with_same_site_policy(SameSite::Strict)
.with_session_renewal_strategy(SessionRenewalStrategy::AutomaticRenewal {
time_to_live: Duration::hours(24),
maximum_remaining_time_to_live_for_renewal: Duration::hours(20),
})
.with_secure(true);
Sets the url path for which the session cookie is valid. Defaults to "/"
.
Sets the name of the session cookie. Defaults to "id"
.
For security reasons, choose a generic name, and ideally just stick with the default.
Sets the domain for which the session cookie is valid. Defaults to None
.
sourcepub fn with_same_site_policy(self, policy: SameSite) -> Self
pub fn with_same_site_policy(self, policy: SameSite) -> Self
Sets the same-site policy of the session cookie. Defaults to
SameSite::Strict
.
For security reasons, do not change this.
sourcepub fn with_session_renewal_strategy(
self,
session_renewal_strategy: SessionRenewalStrategy
) -> Self
pub fn with_session_renewal_strategy( self, session_renewal_strategy: SessionRenewalStrategy ) -> Self
Sets the renewal strategy for sessions.
See the members of SessionRenewalStrategy
for more details.
Defaults to AutomaticRenewal
with a ttl of 24 hours
and an automatic renewal delay of 4 hours.
sourcepub fn with_secure(self, secure: bool) -> Self
pub fn with_secure(self, secure: bool) -> Self
Sets the secure
attribute for the session cookie. Defaults to true
.
For security reasons, do not set this to false
.