18 releases

0.5.0-beta.1 Apr 2, 2021
0.4.1 Mar 21, 2021
0.4.0 Sep 11, 2020
0.4.0-alpha.1 Mar 14, 2020
0.1.0-alpha.2 Mar 30, 2019

#2 in #session

Download history 789/week @ 2021-01-01 1276/week @ 2021-01-08 1372/week @ 2021-01-15 1752/week @ 2021-01-22 2246/week @ 2021-01-29 2005/week @ 2021-02-05 1729/week @ 2021-02-12 2070/week @ 2021-02-19 1973/week @ 2021-02-26 1935/week @ 2021-03-05 1565/week @ 2021-03-12 1752/week @ 2021-03-19 1436/week @ 2021-03-26 1442/week @ 2021-04-02 1589/week @ 2021-04-09 1383/week @ 2021-04-16

7,303 downloads per month
Used in 20 crates (14 directly)

MIT/Apache

36KB
682 lines

actix-session

Sessions for Actix Web.

crates.io Documentation Apache 2.0 or MIT licensed Dependency Status

Documentation & Resources


lib.rs:

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
}

Dependencies

~21MB
~488K SLoC