pub struct SessionLayer<Store> { /* private fields */ }Expand description
Layer that provides cookie-based sessions.
Implementationsยง
Sourceยงimpl<Store: SessionStore> SessionLayer<Store>
impl<Store: SessionStore> SessionLayer<Store>
Sourcepub fn new(store: Store, secret: &[u8]) -> Self
๐Deprecated since 0.6.0: Development of axum-sessions has moved to the tower-sessions crate. Please consider migrating.
pub fn new(store: Store, secret: &[u8]) -> Self
Creates a layer which will attach a SessionHandle to requests via an
extension. This session is derived from a cryptographically signed
cookie. When the client sends a valid, known cookie then the session is
hydrated from this. Otherwise a new cookie is created and returned in
the response.
The default behaviour is to enable โguestโ sessions with
PersistencePolicy::Always.
ยงPanics
SessionLayer::new will panic if the secret is less than 64 bytes.
ยงCustomization
The configuration of the session may be adjusted according to the needs of your application:
SessionLayer::new(
MemoryStore::new(),
b"please do not hardcode your secret; instead use a
cryptographically secure value",
)
.with_cookie_name("your.cookie.name")
.with_cookie_path("/some/path")
.with_cookie_domain("www.example.com")
.with_same_site_policy(SameSite::Lax)
.with_session_ttl(Some(Duration::from_secs(60 * 5)))
.with_persistence_policy(PersistencePolicy::Always)
.with_http_only(true)
.with_secure(true);Sourcepub fn with_persistence_policy(self, policy: PersistencePolicy) -> Self
pub fn with_persistence_policy(self, policy: PersistencePolicy) -> Self
When true, a session cookie will always be set. When false the
session data must be modified in order for it to be set. Defaults to
true.
Sets a cookie for the session. Defaults to "/".
Sets a cookie name for the session. Defaults to "sid".
Sets a cookie domain for the session. Defaults to None.
Sourcepub fn with_same_site_policy(self, policy: SameSite) -> Self
pub fn with_same_site_policy(self, policy: SameSite) -> Self
Sets a cookie same site policy for the session. Defaults to
SameSite::Strict.
Sourcepub fn with_session_ttl(self, session_ttl: Option<Duration>) -> Self
pub fn with_session_ttl(self, session_ttl: Option<Duration>) -> Self
Sets a cookie time-to-live (ttl) for the session. Defaults to
Duration::from_secs(60 * 60 * 24); one day.
Sourcepub fn with_http_only(self, http_only: bool) -> Self
pub fn with_http_only(self, http_only: bool) -> Self
Sets a cookie HttpOnly attribute for the session. Defaults to true.
Sourcepub fn with_secure(self, secure: bool) -> Self
pub fn with_secure(self, secure: bool) -> Self
Sets a cookie secure attribute for the session. Defaults to true.
Trait Implementationsยง
Sourceยงimpl<Store: Clone> Clone for SessionLayer<Store>
impl<Store: Clone> Clone for SessionLayer<Store>
Sourceยงfn clone(&self) -> SessionLayer<Store>
fn clone(&self) -> SessionLayer<Store>
1.0.0 ยท Sourceยงfn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more