Attempt to integrate leptos-keycloak-auth

This commit is contained in:
Thomas Gideon 2024-09-28 10:49:12 -04:00
parent b2e55008dd
commit b990c545e6
5 changed files with 689 additions and 10 deletions

55
src/app/auth.rs Normal file
View file

@ -0,0 +1,55 @@
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>
}
}

View file

@ -1,7 +1,9 @@
use super::series::{add::AddSeries, DeleteSeries, SeriesList};
use leptos::*;
use leptos_router::*;
use super::series::{add::AddSeries, DeleteSeries, SeriesList};
use crate::app::auth::Protected;
#[component]
pub fn HomePage() -> impl IntoView {
let add_series = create_server_action::<AddSeries>();
@ -11,6 +13,7 @@ pub fn HomePage() -> impl IntoView {
provide_context(delete_series);
view! {
<Protected />
<div class="row">
<div class="col">
<SeriesList />

View file

@ -7,6 +7,7 @@ use leptos::*;
use leptos_meta::*;
use leptos_router::*;
mod auth;
mod home;
mod series;