1 unstable release
new 0.1.0 | Dec 19, 2024 |
---|
#1 in #safer
7KB
51 lines
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