Skip to main content

Crate ntex_session

Crate ntex_session 

Source
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§

CookieSession
Use cookies for session storage.
CookieSessionMiddleware
Cookie session middleware service, created by CookieSession.
Session
The high-level interface you use to read and modify session data.

Enums§

CookieSessionError
Errors that can occur during handling cookie session.
SessionStatus
Status of the session after the request is handled.

Traits§

UserSession
Helper trait that allows to get the session of a request.