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 {
|
AuthState {
|
||||||
is_authenticated: None,
|
is_authenticated: None,
|
||||||
..
|
..
|
||||||
@@ -91,6 +91,5 @@ pub fn protected_route(props: &ProtectedRouteProps) -> Html {
|
|||||||
props.children.clone().into()
|
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::*;
|
use yew::prelude::*;
|
||||||
|
|
||||||
#[component(Home)]
|
#[component(Home)]
|
||||||
pub fn home_component() -> Html {
|
pub fn home_component() -> Html {
|
||||||
let counter = use_state(|| 0);
|
let is_admin = use_state(|| None::<bool>);
|
||||||
let onclick = {
|
|
||||||
let counter = counter.clone();
|
|
||||||
move |_| {
|
|
||||||
let value = *counter + 1;
|
|
||||||
counter.set(value);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
html! {
|
{
|
||||||
<div>
|
let is_admin = is_admin.clone();
|
||||||
<button {onclick}>{ "+1" }</button>
|
use_effect_with((), move |_| {
|
||||||
<p>{ *counter }</p>
|
spawn_local(async move {
|
||||||
</div>
|
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 gloo_net::http::Request;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use wasm_bindgen_futures::spawn_local;
|
use wasm_bindgen_futures::spawn_local;
|
||||||
use yew::prelude::*;
|
use yew::prelude::*;
|
||||||
use yew_router::prelude::use_navigator;
|
use yew_router::prelude::use_navigator;
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
// #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||||
pub struct User {
|
// pub struct User {
|
||||||
pub id: i16,
|
// pub id: i16,
|
||||||
pub last_name: String,
|
// pub last_name: String,
|
||||||
pub first_name: String,
|
// pub first_name: String,
|
||||||
pub username: String,
|
// pub username: String,
|
||||||
pub is_admin: bool,
|
// pub is_admin: bool,
|
||||||
pub pwd: String,
|
// pub pwd: String,
|
||||||
}
|
// }
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||||
pub struct UserCreateScheme {
|
pub struct UserCreateScheme {
|
||||||
|
|||||||
Reference in New Issue
Block a user