Crate actix_session[][src]

Sessions for Actix Web.

Provides a general solution for session management. Session middleware could provide different implementations which could be accessed via general session API.

This crate provides a general solution for session management and includes a cookie backend. Other backend implementations can be built to use persistent or key-value stores, for example.

In general, some session middleware, such as a CookieSession is initialized and applied. To access session data, the Session extractor must be used. This extractor allows reading modifying session data.

use actix_web::{web, App, HttpServer, HttpResponse, Error};
use actix_session::{Session, CookieSession};

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

    Ok("Welcome!")
}

#[actix_rt::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(
        || App::new()
            // create cookie based session middleware
            .wrap(CookieSession::signed(&[0; 32]).secure(false))
            .default_service(web::to(|| HttpResponse::Ok())))
        .bind(("127.0.0.1", 8080))?
        .run()
        .await
}

Structs

CookieSession

Use cookies for session storage.

Session

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

Enums

SessionStatus

Status of a Session.

Traits

UserSession

Extraction of a Session object.