Crates.io | actix-session-ext |
lib.rs | actix-session-ext |
version | 0.1.0 |
source | src |
created_at | 2024-12-19 14:17:37.626012+00 |
updated_at | 2024-12-19 14:17:37.626012+00 |
description | An extension library for safer session interface |
homepage | https://github.com/alekece/actix-session-ext |
repository | https://github.com/alekece/actix-session-ext |
max_upload_size | |
id | 1489205 |
size | 15,237 |
The actix-session-ext
crate provides a safer actix_session::Session
interface thanks to typed key.
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))
}
Licensed under MIT license (LICENSE or http://opensource.org/licenses/MIT)