55 lines
1.7 KiB
Rust
55 lines
1.7 KiB
Rust
use leptos::*;
|
|
use leptos_keycloak_auth::{
|
|
use_keycloak_auth, Authenticated, KeycloakAuth, UseKeycloakAuthOptions,
|
|
};
|
|
|
|
#[component]
|
|
// TODO figure out why passing children results in incorrect Fn/FnOnce type when trying to invoke
|
|
// children
|
|
pub fn Protected() -> impl IntoView {
|
|
// Note: These values should be served from environment variables to be overwritten in production.
|
|
let auth = use_keycloak_auth(UseKeycloakAuthOptions {
|
|
keycloak_server_url: "http://localhost:8081/".parse().unwrap(),
|
|
realm: "peculiar".to_owned(),
|
|
client_id: "watch".to_owned(),
|
|
post_login_redirect_url: "http://localhost:3000/".parse().unwrap(),
|
|
post_logout_redirect_url: "http://localhost:3000/".parse().unwrap(),
|
|
scope: Some("openid".to_string()),
|
|
advanced: Default::default(),
|
|
});
|
|
|
|
let user_name = Signal::derive(move || {
|
|
auth.id_token_claims
|
|
.get()
|
|
.map(|claims| claims.name.clone())
|
|
.unwrap_or_default()
|
|
});
|
|
|
|
view! {
|
|
<Authenticated unauthenticated=move || view! { <Login /> }>
|
|
<div>
|
|
"Hello, " {move || user_name.get()}
|
|
</div>
|
|
</Authenticated>
|
|
}
|
|
}
|
|
|
|
#[component]
|
|
pub fn Login() -> impl IntoView {
|
|
let auth = expect_context::<KeycloakAuth>();
|
|
let login_url = Signal::derive(move || {
|
|
auth.login_url
|
|
.get()
|
|
.map(|url| url.to_string())
|
|
.unwrap_or_default()
|
|
});
|
|
let login_disabled = Signal::derive(move || auth.login_url.get().is_none());
|
|
|
|
view! {
|
|
<h1>"Unauthenticated"</h1>
|
|
|
|
<a href={ move || login_url.get() } target="self" disabled={ move || login_disabled.get() }>
|
|
"Log in"
|
|
</a>
|
|
}
|
|
}
|