Skip to main content

session

Function session 

Source
pub async fn session() -> Result<Session, CfError>
Expand description

Load the session for the current request.

Async on first call (reads from KV/D1), cached on subsequent calls. Returns a zero-sized Session handle for sync get/set/remove operations.

§Errors

Returns CfError if:

  • Session middleware is not configured (no .session() on Handler)
  • The backend read fails (KV/D1 unavailable)
  • Session data cannot be deserialized

§Example

use dioxus_cloudflare::prelude::*;

#[server]
pub async fn get_user() -> Result<String, ServerFnError> {
    let session = cf::session().await?;
    let user: Option<String> = session.get("user_id")?;
    Ok(user.unwrap_or_else(|| "not logged in".into()))
}