Skip to main content

require_role

Function require_role 

Source
pub fn require_role<S>(
    role: impl Into<String>,
) -> impl Fn(State<S>, Request<Body>, Next) -> Pin<Box<dyn Future<Output = Response> + Send>> + Clone + Send + 'static
where Arc<dyn AuthClient>: FromRef<S>, S: Send + Sync + Clone + 'static,
Expand description

Middleware factory that requires the authenticated user to have a specific role.

Builds on require_auth: first validates the session (inserting User into extensions), then checks the role. Returns 401 if not authenticated, 403 if authenticated but missing the role.

Usage:

use axum::middleware;

let app = Router::new()
    .route("/admin", get(handler))
    .layer(middleware::from_fn_with_state(
        ath.clone(),
        require_role("admin"),
    ));