Skip to main content

Module session

Module session 

Source
Expand description

Sessions carried by a cookie. Sessions: per-visitor state carried by a cookie.

A SessionStore trait plus a cookie-backed implementation. The session is a string map, seeded into the call by Sessions and read with the Session extractor.

use churust_core::{Call, Churust, Session, Sessions, TestClient};
let app = Churust::server()
    .install(Sessions::cookie("change-me-in-production"))
    .routing(|r| {
        r.get("/visit", |s: Session| async move {
            let n: u32 = s.get("count").and_then(|v| v.parse().ok()).unwrap_or(0);
            s.set("count", (n + 1).to_string());
            format!("visit {}", n + 1)
        });
    })
    .build();

let res = TestClient::new(app).get("/visit").send().await;
assert_eq!(res.text(), "visit 1");
assert!(res.header("set-cookie").is_some());

Structs§

CookieStore
Keeps the whole session in the cookie, signed with HMAC-SHA256 so a client cannot edit it.
Session
A handle to the current visitor’s session.
Sessions
Installs session handling. See the module docs.

Constants§

SESSION_ID_KEY
The reserved key under which a server-side SessionStore records which row, document or Redis key this session is.

Traits§

SessionStore
Where session contents live between requests.