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