- Rust 100%
| .woodpecker | ||
| assets | ||
| src | ||
| .gitignore | ||
| Cargo.lock | ||
| Cargo.toml | ||
| LICENSE | ||
| README.md | ||
Leptos Auth Router
A OpenId-Connect (oidc) authentication router with server-function integration for Leptos + Axum apps.
This crate provides:
- Login and callback routes
- Session management via signed cookies
- Middleware to protect routes
Quick Start
Enable authentication for Leptos routes
use leptos_auth_router::config::{Config, AuthConfig, RouteConfig};
use axum::Router;
// Configure authentication
let auth_config = AuthConfig {
base_url: url::Url::parse("http://example.com").expect("Invalid base url"),
issuer_url: url::Url::parse("https://auth.example.com/auth/v1").expect("Invalid issuer url"),
client_id: "test-client".to_string(),
client_secret: "your-secret".to_string(),
cookie_secret: "a-very-long-random-secret".to_string(),
};
// Configure routes
/* Default implementation */
let route_config = RouteConfig {
prefix: "auth".to_owned(),
login_path: "login".to_owned(),
callback_path: "callback".to_owned(),
};
// Extend your Axum app with authentication
let app = Router::new()
.leptos_routes(&leptos_options, routes, {
let leptos_options = leptos_options.clone();
move || shell(leptos_options.clone())
})
.fallback(leptos_axum::file_and_error_handler(shell))
.with_state(leptos_options)
.use_leptos_auth(Config {
auth: auth_config,
route: route_config,
})
.await;
Access session data in server functions
pub fn App() -> impl IntoView {
let session_data =
LocalResource::new(move || async { leptos_auth_router::session::get_auth_session().await });
view! {
<p>{move || format!("{:?}", session_data.get())}</p>
}
}
Leptos compatibility
| Crate version | Compatible Leptos version |
|---|---|
| 0.1 | 0.8 |
Installation
📌 If your are adding this crate to your server application, the
ssrfeature must be enabled.
cargo add leptos-auth-router
Note: Authentication is handled completely on the server.
Using Leptos starter templates like start-axum may cause server-client separation issues.
For proper server-client separation, use the start-axum-workspace template.
⚠️ Important
This crate is experimental and may contains security issues.
Use at your own risk!
No liability is assumed for any damages or issues resulting from its use.
License
leptos-auth-router is distributed under the MIT License.
For more information, see the LICENSE file.
