Struct actix_session::Session

source ·
pub struct Session(_);
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 actix_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", count + 1)?;
    } else {
        session.insert("counter", 1)?;
    }

    Ok("Welcome!")
}

You can also retrieve a Session object from an HttpRequest or a ServiceRequest using SessionExt.

Implementations§

Get a value from the session.

It returns an error if it fails to deserialize as T the JSON value associated with key.

Get all raw key-value data from the session.

Note that values are JSON encoded.

Returns session status.

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.

It returns an error if it fails to serialize value to JSON.

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.

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more

Extractor implementation for Sessions.

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. Read more
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

Returns the argument unchanged.

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

Calls U::from(self).

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

Should always be Self
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
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