Home page changes depending on role
This commit is contained in:
@@ -64,7 +64,7 @@ pub fn protected_route(props: &ProtectedRouteProps) -> Html {
|
||||
});
|
||||
}
|
||||
|
||||
match (*auth_state) {
|
||||
match *auth_state {
|
||||
AuthState {
|
||||
is_authenticated: None,
|
||||
..
|
||||
@@ -91,6 +91,5 @@ pub fn protected_route(props: &ProtectedRouteProps) -> Html {
|
||||
props.children.clone().into()
|
||||
}
|
||||
}
|
||||
_ => html! { <div>{ "Checking permissions..." }</div> },
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,21 +1,37 @@
|
||||
use gloo_net::http::Request;
|
||||
use wasm_bindgen_futures::spawn_local;
|
||||
use yew::prelude::*;
|
||||
|
||||
#[component(Home)]
|
||||
pub fn home_component() -> Html {
|
||||
let counter = use_state(|| 0);
|
||||
let onclick = {
|
||||
let counter = counter.clone();
|
||||
move |_| {
|
||||
let value = *counter + 1;
|
||||
counter.set(value);
|
||||
}
|
||||
};
|
||||
let is_admin = use_state(|| None::<bool>);
|
||||
|
||||
html! {
|
||||
<div>
|
||||
<button {onclick}>{ "+1" }</button>
|
||||
<p>{ *counter }</p>
|
||||
</div>
|
||||
{
|
||||
let is_admin = is_admin.clone();
|
||||
use_effect_with((), move |_| {
|
||||
spawn_local(async move {
|
||||
let response = Request::get("/api/users/current")
|
||||
.credentials(web_sys::RequestCredentials::Include)
|
||||
.send()
|
||||
.await;
|
||||
|
||||
match response {
|
||||
Ok(resp) if resp.status() == 200 => {
|
||||
let user_data: serde_json::Value = resp.json().await.unwrap_or_default();
|
||||
let admin_value = user_data["data"]["is_admin"].as_bool();
|
||||
is_admin.set(admin_value);
|
||||
}
|
||||
_ => is_admin.set(Some(false)),
|
||||
}
|
||||
});
|
||||
|| ()
|
||||
});
|
||||
}
|
||||
|
||||
match *is_admin {
|
||||
None => html! { <div>{ "Loading..." }</div> },
|
||||
Some(true) => html! { <div>{ "You are admin" }</div> },
|
||||
Some(false) => html! { <div>{ "You are a normal user" }</div> },
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,20 +1,18 @@
|
||||
use std::net::ToSocketAddrs;
|
||||
|
||||
use gloo_net::http::Request;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use wasm_bindgen_futures::spawn_local;
|
||||
use yew::prelude::*;
|
||||
use yew_router::prelude::use_navigator;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub struct User {
|
||||
pub id: i16,
|
||||
pub last_name: String,
|
||||
pub first_name: String,
|
||||
pub username: String,
|
||||
pub is_admin: bool,
|
||||
pub pwd: String,
|
||||
}
|
||||
// #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
// pub struct User {
|
||||
// pub id: i16,
|
||||
// pub last_name: String,
|
||||
// pub first_name: String,
|
||||
// pub username: String,
|
||||
// pub is_admin: bool,
|
||||
// pub pwd: String,
|
||||
// }
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub struct UserCreateScheme {
|
||||
|
||||
Reference in New Issue
Block a user