cashmere

cashmere

how to create a custom actix web return type

Preface

To create a custom Actix Web return type, you need to implement the Responder trait for your type.

Example

pub async fn handle_login(session: &Session) -> Result<bool> {
    match session.get::<String>("role") {
        Ok(Some(role)) => match role.as_str() {
            "administration" | "ticket_support" | "super_host" | "host" | "tenant" => Ok(true),
            "unverified" => Err(actix_web::error::InternalError::from_response(
                "Redirect to verify",
                HttpResponse::SeeOther()
                    .append_header(("Location", "/verify"))
                    .finish(),
            )
            .into()),
            _ => Err(actix_web::error::ErrorUnauthorized("Invalid role")),
        },
        Ok(None) => Err(actix_web::error::ErrorUnauthorized("No role in session")),
        Err(_) => Err(actix_web::error::ErrorUnauthorized("Session error")),
    }
}

References