pub struct Session { /* private fields */ }Expand description
The request’s session.
Implementations§
Source§impl Session
impl Session
Sourcepub fn detached() -> Self
pub fn detached() -> Self
A session that is not backed by anything. Reads are empty and writes report the missing middleware.
pub fn id(&self) -> String
pub fn is_active(&self) -> bool
pub fn get<T: DeserializeOwned>(&self, key: &str) -> Result<Option<T>>
pub fn put<T: Serialize>(&self, key: impl Into<String>, value: T) -> Result<()>
pub fn has(&self, key: &str) -> bool
pub fn forget(&self, key: &str) -> Result<()>
Sourcepub fn flash<T: Serialize>(
&self,
key: impl Into<String>,
value: T,
) -> Result<()>
pub fn flash<T: Serialize>( &self, key: impl Into<String>, value: T, ) -> Result<()>
Store a value readable on the next request only, then discarded.
This is what makes the post-redirect-get pattern work: an action that fails validation flashes the errors and redirects, and the GET that follows reads them once. Nothing has to remember to clean up.
Flash is deliberately separate from Session::put. A value that
should survive exactly one hop and a value that should persist are
different intentions, and conflating them is how stale error messages
end up rendered on unrelated pages.
Sourcepub fn flashed<T: DeserializeOwned>(&self, key: &str) -> Result<Option<T>>
pub fn flashed<T: DeserializeOwned>(&self, key: &str) -> Result<Option<T>>
Read a value flashed by the previous request.
Reading does not consume it: within one request the same flash can be read by middleware and by an action. It is discarded when the next request ages the bag.
Sourcepub fn destroy(&self) -> Result<()>
pub fn destroy(&self) -> Result<()>
Invalidate the session entirely: the store entry is removed and the cookie cleared.
Sourcepub fn set_subject(&self, subject: impl Into<String>) -> Result<()>
pub fn set_subject(&self, subject: impl Into<String>) -> Result<()>
Record who this session belongs to.
Does not rotate the id — call Session::regenerate on login to
defeat session fixation, which is what login_as does for you.
pub fn subject(&self) -> Option<String>
Sourcepub fn regenerate(&self) -> Result<()>
pub fn regenerate(&self) -> Result<()>
Give the session a fresh id, keeping its contents.
Do this whenever privilege changes. Without it, an attacker who fixed a victim’s session id before login still holds a valid id afterwards.