pub struct Session { /* private fields */ }Expand description
A session handle injected into request extensions.
Implementations§
Source§impl Session
impl Session
Sourcepub fn get<T: DeserializeOwned>(&self, key: &str) -> Option<T>
pub fn get<T: DeserializeOwned>(&self, key: &str) -> Option<T>
Reads a value from the session.
Sourcepub fn set<T: Serialize>(&self, key: &str, value: T)
pub fn set<T: Serialize>(&self, key: &str, value: T)
Stores a value in the session, marking it dirty.
Sourcepub fn clear(&self)
pub fn clear(&self)
Empties the session keeping its id stable. Use this when you want the
session to live on (e.g. clearing temporary state) but the cookie should
keep being refreshed. For logout flows that should remove the cookie
from the browser, use Self::destroy instead.
Sourcepub fn destroy(&self)
pub fn destroy(&self)
Marks the session for destruction: the server-side entry is removed and
the response Set-Cookie carries Max-Age=0 with a past Expires so the
user agent drops it. Pair this with whatever logout response your
application returns.
Sourcepub fn rotate(&self)
pub fn rotate(&self)
Forces a fresh session id on the next response. Call this after privilege transitions (login / role change) to defend against fixation attacks.
Sourcepub fn rotation_requested(&self) -> bool
pub fn rotation_requested(&self) -> bool
True if Session::rotate has been called on this handle since the
session middleware created it. Surfaced as public API so paired
middleware (notably CSRF) can mint fresh derivative tokens on the same
response that emits the rotated session id.