pub struct Session(/* private fields */);Expand description
The primary interface to access and modify session state.
Session is an extractor—you can specify it as an input type for your
request handlers and it will be automatically extracted from the incoming request.
use serde_json::Value;
use actix_extended_session::Session;
async fn index(session: Session) -> actix_web::Result<&'static str> {
// access session data
if let Some(count) = session.get::<i32>("counter")? {
session.insert("counter", Value::from(count + 1));
} else {
session.insert("counter", Value::from(1));
}
Ok("Welcome!")
}You can also retrieve a Session object from an HttpRequest or a ServiceRequest using
SessionExt.
Implementations§
Source§impl Session
impl Session
Sourcepub fn get<T: DeserializeOwned>(
&self,
key: &str,
) -> Result<Option<T>, SessionGetError>
pub fn get<T: DeserializeOwned>( &self, key: &str, ) -> Result<Option<T>, SessionGetError>
Get a value from the session.
It returns an error if it fails to parse as T the JSON value associated with key.
Sourcepub fn entries(&self) -> Ref<'_, Map<String, Value>>
pub fn entries(&self) -> Ref<'_, Map<String, Value>>
Get all raw key-value data from the session.
Note that values are JSON encoded.
Sourcepub fn status(&self) -> SessionStatus
pub fn status(&self) -> SessionStatus
Returns session status.
Sourcepub fn insert(&self, key: impl Into<String>, value: Value)
pub fn insert(&self, key: impl Into<String>, value: Value)
Inserts a key-value pair into the session.
Sourcepub fn remove(&self, key: &str) -> Option<Value>
pub fn remove(&self, key: &str) -> Option<Value>
Remove value from the session.
If present, the JSON encoded value is returned.
Sourcepub fn set_lifecycle(&self, next_lifecycle: SessionLifecycle)
pub fn set_lifecycle(&self, next_lifecycle: SessionLifecycle)
Sets the lifecycle of the session cookie.
Sourcepub fn get_lifecycle(&self) -> SessionLifecycle
pub fn get_lifecycle(&self) -> SessionLifecycle
Returns the lifecycle of the session cookie.
Trait Implementations§
Source§impl FromRequest for Session
Extractor implementation for Sessions.
impl FromRequest for Session
Extractor implementation for Sessions.
§Examples
use actix_extended_session::Session;
use serde_json::Value;
#[get("/")]
async fn index(session: Session) -> Result<impl Responder> {
// access session data
if let Some(count) = session.get::<i32>("counter")? {
session.insert("counter", Value::from(count + 1));
} else {
session.insert("counter", Value::from(1));
}
let count = session.get::<i32>("counter")?.unwrap();
Ok(format!("Counter: {}", count))
}Source§fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future
Self from request parts asynchronously.