livewire-rust 0.1.3

Livewire for Rust: server-rendered reactive components for Laravel for Rust.
Documentation
//! Guards that run when the browser drives a component a page mounted.
//!
//! A page's route middleware runs once, when the page is requested. The browser then drives the
//! component through the update endpoint, which is not that route. A [`PersistentMiddleware`] runs
//! on every such update with the request that arrived and the page the component was mounted from,
//! so a guard — a login check, a tenant check, a maintenance flag — holds for the component's whole
//! life instead of only its first render.

use laravel::http::{Request, Response};

use crate::context::PageContext;

/// A guard an application registers once, and an update runs before the component renders.
#[::livewire::__private::async_trait]
pub trait PersistentMiddleware: Send + Sync {
    /// Returns a response to refuse the update with, or `None` to let it run.
    async fn handle(&self, request: &Request, page: &PageContext) -> Option<Response>;
}

/// The session a request carries, for a guard that checks it.
#[must_use]
pub fn session(key: &str) -> Option<serde_json::Value> {
    let _ = laravel_core::context::Context::current()?;

    crate::session::get(key)
}