From c64a7a640ac8c59eb6339f0a06d2ad2efab3fd11 Mon Sep 17 00:00:00 2001 From: Kjetil Orbekk Date: Fri, 7 Oct 2022 16:59:29 -0400 Subject: Start working on authentication --- webapp/Cargo.toml | 2 +- webapp/src/components.rs | 2 + webapp/src/components/app_context_provider.rs | 50 ++++++++++++++++++++++ webapp/src/main.rs | 60 ++++++++++++--------------- 4 files changed, 80 insertions(+), 34 deletions(-) create mode 100644 webapp/src/components/app_context_provider.rs (limited to 'webapp') diff --git a/webapp/Cargo.toml b/webapp/Cargo.toml index fe7addb..5411ccd 100644 --- a/webapp/Cargo.toml +++ b/webapp/Cargo.toml @@ -18,7 +18,7 @@ regex = "1.0" lazy_static = "1.4" gloo-net = "0.2.4" wasm-bindgen-futures = "0.4.33" -data = { path = "../data" } +protocol = { path = "../protocol" } yew-router = "0.16.0" [dev-dependencies] diff --git a/webapp/src/components.rs b/webapp/src/components.rs index 80b5a91..224177b 100644 --- a/webapp/src/components.rs +++ b/webapp/src/components.rs @@ -1,5 +1,6 @@ use crate::card::Suit; +mod app_context_provider; mod bidding; mod bidding_box; mod bidding_table; @@ -10,6 +11,7 @@ mod show_bid; mod trick_in_play; mod tricks_played; +pub use self::app_context_provider::*; pub use self::bidding::*; pub use self::bidding_box::*; pub use self::bidding_table::*; diff --git a/webapp/src/components/app_context_provider.rs b/webapp/src/components/app_context_provider.rs new file mode 100644 index 0000000..f2938ff --- /dev/null +++ b/webapp/src/components/app_context_provider.rs @@ -0,0 +1,50 @@ +use gloo_net::http::Request; +use protocol::UserInfo; +use std::rc::Rc; +use yew::prelude::*; + +#[derive(Clone, Debug, PartialEq)] +pub struct AppContext { + pub user: Option, +} + +#[derive(Properties, Clone, PartialEq)] +pub struct Props { + pub children: Children, +} + +#[function_component(AppContextProvider)] +pub fn app_context_provider(props: &Props) -> Html { + let context: UseStateHandle>> = use_state(|| None); + + { + let context = context.clone(); + use_effect_with_deps( + move |_| { + wasm_bindgen_futures::spawn_local(async move { + let user_info: Option = Request::get("/api/user/info") + .send() + .await + .unwrap() + .json() + .await + .unwrap(); + context.set(Some(Rc::new(AppContext { user: user_info }))); + }); + || () + }, + (), + ); + } + + match &*context { + None => html! { +

{ "Loading app..." }

+ }, + Some(context) => html! { + > {context}> + { for props.children.iter() } + >> + }, + } +} diff --git a/webapp/src/main.rs b/webapp/src/main.rs index 6fcf59f..a3fcebc 100644 --- a/webapp/src/main.rs +++ b/webapp/src/main.rs @@ -1,4 +1,5 @@ -use data::MyMessage; +use std::rc::Rc; + #[allow(unused_imports)] use log::{debug, error, info, warn}; use yew::prelude::*; @@ -6,7 +7,7 @@ use yew_router::prelude::*; pub mod bridge_engine; pub mod card; pub mod components; -use components::Game; +use components::{AppContextProvider, AppContext, Game}; use gloo_net::http::Request; extern crate wee_alloc; @@ -30,48 +31,41 @@ fn main() { #[function_component(App)] pub fn app() -> Html { - let msg = use_state(|| "".to_string()); - { - let msg = msg.clone(); - use_effect_with_deps( - move |_| { - wasm_bindgen_futures::spawn_local(async move { - let m: MyMessage = Request::get("/api/test") - .send() - .await - .unwrap() - .json() - .await - .unwrap(); - msg.set(m.message) - }); - || () - }, - (), - ) + html! { + <> + + + render={Switch::render(switch)} /> + + + } +} + +#[function_component(Home)] +fn home() -> Html { + let ctx = use_context::>().unwrap(); - info!("Got message from server: {}", &*msg); + let user = match &ctx.user { + Some(userinfo) => html! { +

{ format!("Logged in as {}", userinfo.username) }

+ }, + None => html! {

{ "Not logged in" }

}, + }; html! { <> - - render={Switch::render(switch)} /> - + { user } +

+ to={Route::Playground}>{ "Playground" }> +

} } fn switch(routes: &Route) -> Html { match routes { - Route::Home => html! { - <> -

{ "Hello!" }

-

- to={Route::Playground}>{ "Playground" }> -

- - }, + Route::Home => html!{ }, Route::Playground => html! {
}, -- cgit v1.2.3