validate_admin

Function validate_admin 

Source
pub async fn validate_admin(
    cookies: CookieJar,
    __arg1: State<Arc<AppState>>,
    request: Request<Body>,
    next: Next,
) -> Result<impl IntoResponse, (StatusCode, Json<Value>)>
Expand description

Axum middleware to validate JWT token and ensure the authenticated user has admin privileges.

This middleware first performs all checks of validate_token: extracting, decoding, and validating the JWT via decode_token, and fetching the associated User from the database. Additionally, it verifies that the fetched user has is_admin set to true. Returns a FilteredUser (converted via filter_user) in the request extensions if both authentication and admin status are valid.

If the user is not authenticated or not an administrator, it returns an appropriate error response (401 Unauthorized or 403 Forbidden).

§Arguments

  • cookies: The CookieJar from the request.
  • State(data): Application state containing AppState.
  • mut request: The incoming HTTP request, which will have admin user data injected.
  • next: The next middleware or handler in the chain.

§Returns

  • Ok(impl IntoResponse): If validation and admin check succeed, the request proceeds.
  • Err((StatusCode, Json<serde_json::Value>)): An error response if validation fails or the user is not an admin.