Frontend initial admin setup
This commit is contained in:
@@ -2,6 +2,8 @@ mod auth;
|
||||
mod pages;
|
||||
use crate::auth::ProtectedRoute;
|
||||
use crate::pages::*;
|
||||
use gloo_net::http::Request;
|
||||
use wasm_bindgen_futures::spawn_local;
|
||||
use yew::prelude::*;
|
||||
use yew_router::prelude::*;
|
||||
|
||||
@@ -19,6 +21,8 @@ enum Route {
|
||||
Register,
|
||||
#[at("/login")]
|
||||
Login,
|
||||
#[at("/setup")]
|
||||
Setup,
|
||||
#[at("/users")]
|
||||
AllUsers,
|
||||
#[at("/users/:id")]
|
||||
@@ -49,6 +53,47 @@ fn sidebar_shell(props: &SidebarShellProps) -> Html {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Properties, PartialEq)]
|
||||
pub struct AdminCheckWrapperProps {
|
||||
pub children: Children,
|
||||
}
|
||||
|
||||
#[component(AdminCheckWrapper)]
|
||||
fn admin_check_wrapper(props: &AdminCheckWrapperProps) -> Html {
|
||||
let admin_exists = use_state(|| None::<bool>);
|
||||
let navigator = use_navigator().unwrap();
|
||||
|
||||
{
|
||||
let admin_exists = admin_exists.clone();
|
||||
use_effect_with((), move |_| {
|
||||
let admin_exists = admin_exists.clone();
|
||||
spawn_local(async move {
|
||||
match Request::get("/api/check-admin").send().await {
|
||||
Ok(resp) if resp.status() == 200 => {
|
||||
if let Ok(data) = resp.json::<serde_json::Value>().await {
|
||||
let has_admin = data["has_admin"].as_bool().unwrap_or(false);
|
||||
admin_exists.set(Some(has_admin));
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
admin_exists.set(Some(false));
|
||||
}
|
||||
}
|
||||
});
|
||||
|| ()
|
||||
});
|
||||
}
|
||||
|
||||
match *admin_exists {
|
||||
None => html! { <div>{ "Loading..." }</div> },
|
||||
Some(false) => {
|
||||
navigator.push(&Route::Setup);
|
||||
html! { <div>{ "Redirecting to setup..." }</div> }
|
||||
}
|
||||
Some(true) => props.children.clone().into(),
|
||||
}
|
||||
}
|
||||
|
||||
fn switch(route: Route) -> Html {
|
||||
match route {
|
||||
Route::Home => html! {
|
||||
@@ -87,7 +132,14 @@ fn switch(route: Route) -> Html {
|
||||
</SidebarShell>
|
||||
</ProtectedRoute>
|
||||
},
|
||||
Route::Login => html! { <user::Login/> },
|
||||
Route::Login => html! {
|
||||
<AdminCheckWrapper>
|
||||
<user::Login/>
|
||||
</AdminCheckWrapper>
|
||||
},
|
||||
Route::Setup => html! {
|
||||
<setup::InitialAdminSetup/>
|
||||
},
|
||||
Route::AllUsers => html! {
|
||||
<ProtectedRoute admin_page={true}>
|
||||
<SidebarShell>
|
||||
|
||||
Reference in New Issue
Block a user