livewire/persistent.rs
1//! Guards that run when the browser drives a component a page mounted.
2//!
3//! A page's route middleware runs once, when the page is requested. The browser then drives the
4//! component through the update endpoint, which is not that route. A [`PersistentMiddleware`] runs
5//! on every such update with the request that arrived and the page the component was mounted from,
6//! so a guard — a login check, a tenant check, a maintenance flag — holds for the component's whole
7//! life instead of only its first render.
8
9use laravel::http::{Request, Response};
10
11use crate::context::PageContext;
12
13/// A guard an application registers once, and an update runs before the component renders.
14#[::livewire::__private::async_trait]
15pub trait PersistentMiddleware: Send + Sync {
16 /// Returns a response to refuse the update with, or `None` to let it run.
17 async fn handle(&self, request: &Request, page: &PageContext) -> Option<Response>;
18}
19
20/// The session a request carries, for a guard that checks it.
21#[must_use]
22pub fn session(key: &str) -> Option<serde_json::Value> {
23 let _ = laravel_core::context::Context::current()?;
24
25 crate::session::get(key)
26}