Expand description
User sessions for the ntex web framework.
The crate provides a backend-independent Session API. A session
middleware loads the session state for each request and writes changes
back to the response; handlers access the state through the Session
extractor or the UserSession trait.
Session values are stored as JSON strings and are (de)serialized with
serde_json.
Only a cookie based backend, CookieSession, is provided (enabled by the
default cookie-session feature). Other backends can be built on top of
Session::set_session and Session::get_changes.
§Example
use ntex::web::{self, App};
use ntex_session::{CookieSession, Session};
async fn index(session: Session) -> Result<&'static str, serde_json::Error> {
let count = session.get::<u32>("counter")?.unwrap_or(0);
session.set("counter", count + 1)?;
Ok("Welcome!")
}
let app = App::default()
.middleware(
// Use a secret, stable key in production; disable Secure only for local HTTP.
CookieSession::signed(&[0; 32]).secure(false),
)
.service(web::resource("/").to(index));Structs§
- Cookie
Session - Use cookies for session storage.
- Cookie
Session Middleware - Cookie session middleware service, created by
CookieSession. - Session
- The high-level interface you use to read and modify session data.
Enums§
- Cookie
Session Error - Errors that can occur during handling cookie session.
- Session
Status - Status of the session after the request is handled.
Traits§
- User
Session - Helper trait that allows to get the session of a request.