pub struct Session(_);
Expand description

The high-level interface you use to modify session data.

Session object is obtained with UserSession::get_session. The UserSession trait is implemented for HttpRequest, ServiceRequest, and RequestHead.

use actix_session::Session;
use actix_web::Result;

async fn index(session: Session) -> Result<&'static str> {
    // access session data
    if let Some(count) = session.get::<i32>("counter")? {
        session.insert("counter", count + 1)?;
    } else {
        session.insert("counter", 1)?;
    }

    Ok("Welcome!")
}

Implementations

Get a value from the session.

Get all raw key-value data from the session.

Note that values are JSON encoded.

Inserts a key-value pair into the session.

Any serializable value can be used and will be encoded as JSON in session data, hence why only a reference to the value is taken.

Remove value from the session.

If present, the JSON encoded value is returned.

Remove value from the session and deserialize.

Returns None if key was not present in session. Returns T if deserialization succeeds, otherwise returns un-deserialized JSON string.

Clear the session.

Removes session both client and server side.

Renews the session key, assigning existing session state to new key.

Adds the given key-value pairs to the session on the request.

Values that match keys already existing on the session will be overwritten. Values should already be JSON serialized.

Examples
let mut req = test::TestRequest::default().to_srv_request();

Session::set_session(
    &mut req,
    vec![("counter".to_string(), serde_json::to_string(&0).unwrap())],
);

Returns session status and iterator of key-value pairs of changes.

Trait Implementations

Extractor implementation for Session type.

Examples

use actix_session::Session;

#[get("/")]
async fn index(session: Session) -> Result<impl Responder> {
    // access session data
    if let Some(count) = session.get::<i32>("counter")? {
        session.insert("counter", count + 1)?;
    } else {
        session.insert("counter", 1)?;
    }

    let count = session.get::<i32>("counter")?.unwrap();
    Ok(format!("Counter: {}", count))
}

The associated error which can be returned.

Future that resolves to a Self.

Create a Self from request parts asynchronously.

Create a Self from request head asynchronously. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

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

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

Performs the conversion.

Should always be Self

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

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

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