#session #actix #session-keys #safer #interface #ext #key

actix-session-ext

An extension library for safer session interface

1 unstable release

new 0.1.0 Dec 19, 2024

#1 in #safer

MIT license

7KB
51 lines

crates.io MIT licensed Documentation CI

actix-session-ext

The actix-session-ext crate provides a safer actix_session::Session interface thanks to typed key.

Examples

use actix_web::{Error, Responder, HttpResponse};
use actix_session::Session;
use actix_session_ext::{SessionKey, SessionExt};

// create an actix application and attach the session middleware to it

const USER_KEY: SessionKey<String> = SessionKey::new("user");
const TIMESTAMP_KEY: SessionKey<u64> = SessionKey::new("timestamp");

#[actix_web::post("/login")]
async fn login(session: Session) -> Result<String, Error> {
    session.insert_by_key(USER_KEY, "Dupont".to_owned())?;
    session.insert_by_key(TIMESTAMP_KEY, 1234567890)?;

   Ok("logged in".to_owned())
}

#[actix_web::get("/logged_at")]
async fn logged_at(session: Session) -> Result<String, Error> {
   let timestamp = session.get_by_key(TIMESTAMP_KEY)?.unwrap_or_default();

   Ok(format!("logged at {}", timestamp))
}

License

Licensed under MIT license (LICENSE or http://opensource.org/licenses/MIT)

Dependencies

~16–26MB
~446K SLoC