Skip to content

Framework Integration

Authkestra provides first-class support for both the axum and actix-web web frameworks through lightweight adapter crates (authkestra-axum and authkestra-actix).

Ensure you have enabled the correct web framework feature on the authkestra crate in your Cargo.toml. Also, since session management is stateful, make sure the session feature is enabled.

[dependencies]
authkestra = { version = "0.2.1", features = ["axum", "session"] }

The primary way to interact with an authenticated user in a handler is via the AuthSession extractor. It automatically validates the session and injects the user’s identity.

use authkestra_axum::{AuthSession, AxumError};
use axum::response::{IntoResponse, Json};
use serde_json::json;
pub async fn my_handler(
session: Result<AuthSession, AxumError>
) -> impl IntoResponse {
match session {
Ok(AuthSession(session_data)) => Json(json!({
"message": "Welcome!",
"user_id": session_data.identity.external_id,
})),
Err(_) => Json(json!({ "error": "Not authenticated" }))
}
}

To wire up the endpoints, you use axum_router() or actix_router() on the built Engine.

use authkestra_axum::AxumExt;
let app = Router::new()
.merge(auth_engine.axum_router())
.layer(CookieManagerLayer::new())
.with_state(AppState { auth: auth_engine });