Struct axum_sessions::SessionLayer

source ยท
pub struct SessionLayer<Store> { /* private fields */ }
Expand description

Layer that provides cookie-based sessions.

Implementationsยง

sourceยง

impl<Store: SessionStore> SessionLayer<Store>

source

pub 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.

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);
source

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.

source

pub fn with_same_site_policy(self, policy: SameSite) -> Self

Sets a cookie same site policy for the session. Defaults to SameSite::Strict.

source

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.

source

pub fn with_http_only(self, http_only: bool) -> Self

Sets a cookie HttpOnly attribute for the session. Defaults to true.

source

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>

sourceยง

fn clone(&self) -> SessionLayer<Store>

Returns a copy of the value. Read more
1.0.0 ยท sourceยง

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
sourceยง

impl<Inner, Store: SessionStore> Layer<Inner> for SessionLayer<Store>

ยง

type Service = Session<Inner, Store>

The wrapped service
sourceยง

fn layer(&self, inner: Inner) -> Self::Service

Wrap the given service with the middleware, returning a new service that has been decorated with the middleware.

Auto Trait Implementationsยง

ยง

impl<Store> RefUnwindSafe for SessionLayer<Store>where Store: RefUnwindSafe,

ยง

impl<Store> Send for SessionLayer<Store>where Store: Send,

ยง

impl<Store> Sync for SessionLayer<Store>where Store: Sync,

ยง

impl<Store> Unpin for SessionLayer<Store>where Store: Unpin,

ยง

impl<Store> UnwindSafe for SessionLayer<Store>where Store: UnwindSafe,

Blanket Implementationsยง

sourceยง

impl<T> Any for Twhere T: 'static + ?Sized,

sourceยง

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
sourceยง

impl<T> Borrow<T> for Twhere T: ?Sized,

sourceยง

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
sourceยง

impl<T> BorrowMut<T> for Twhere T: ?Sized,

sourceยง

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
sourceยง

impl<T> From<T> for T

sourceยง

fn from(t: T) -> T

Returns the argument unchanged.

ยง

impl<T> FromRef<T> for Twhere T: Clone,

ยง

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
sourceยง

impl<T> Instrument for T

sourceยง

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
sourceยง

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
sourceยง

impl<T, U> Into<U> for Twhere U: From<T>,

sourceยง

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

sourceยง

impl<T> Same for T

ยง

type Output = T

Should always be Self
sourceยง

impl<T> ToOwned for Twhere T: Clone,

ยง

type Owned = T

The resulting type after obtaining ownership.
sourceยง

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
sourceยง

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
sourceยง

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

ยง

type Error = Infallible

The type returned in the event of a conversion error.
sourceยง

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
sourceยง

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

ยง

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
sourceยง

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
ยง

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

ยง

fn vzip(self) -> V

sourceยง

impl<T> WithSubscriber for T

sourceยง

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
sourceยง

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more