Crate actix_session_ext

Source
Expand description

§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))
}

Structs§

SessionKey
SessionKey<T>, a struct binding a key to its respective type.

Traits§

SessionExt
An extension trait for actix_session::session that provides a safer alternative to session::get, session::insert, session::remove methods. This trait is implemented for actix_session::session and provides methods that take SessionKey<T> instead of raw keys.