From c94105acc35daae5da13300f08b040b99454a526 Mon Sep 17 00:00:00 2001 From: pjht Date: Wed, 16 Oct 2024 15:18:18 -0500 Subject: [PATCH] Add logout functionallity --- Cargo.lock | 1 + common/src/lib.rs | 5 + frontend/Cargo.toml | 1 + frontend/src/components/nav.rs | 86 ++++++++-- frontend/src/main.rs | 5 +- frontend/src/pages.rs | 4 +- frontend/src/pages/chat.rs | 238 +++++++++++++++++++++++++++ frontend/src/pages/home.rs | 290 ++++++++------------------------- frontend/src/pages/login.rs | 64 +++++++- server/src/main.rs | 22 ++- 10 files changed, 468 insertions(+), 248 deletions(-) create mode 100644 frontend/src/pages/chat.rs diff --git a/Cargo.lock b/Cargo.lock index fc0757b..7af49b6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -550,6 +550,7 @@ dependencies = [ "console_error_panic_hook", "gloo 0.11.0", "log", + "serde", "wasm-bindgen", "wasm-logger", "web-sys", diff --git a/common/src/lib.rs b/common/src/lib.rs index 7cdf05f..39239fe 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -6,3 +6,8 @@ pub struct ChatMessage { pub message: String, pub time: DateTime, } + +#[derive(Serialize, Deserialize, Clone, Debug)] +pub struct LoggedInResponse { + pub logged_in: bool, +} diff --git a/frontend/Cargo.toml b/frontend/Cargo.toml index ebf0418..c748238 100644 --- a/frontend/Cargo.toml +++ b/frontend/Cargo.toml @@ -10,6 +10,7 @@ common = { version = "0.1.0", path = "../common" } console_error_panic_hook = "0.1.7" gloo = "0.11.0" log = "0.4.22" +serde = { version = "1.0.210", features = ["derive"] } wasm-bindgen = "0.2.93" wasm-logger = "0.2.0" web-sys = { version = "0.3.70", features = ["Navigator", "WebSocket", "EventListener"] } diff --git a/frontend/src/components/nav.rs b/frontend/src/components/nav.rs index 30aba09..a204c45 100644 --- a/frontend/src/components/nav.rs +++ b/frontend/src/components/nav.rs @@ -1,27 +1,89 @@ -use yew::prelude::*; +use common::LoggedInResponse; +use gloo::net::http::Request; +use yew::{platform::spawn_local, prelude::*}; +use yew_hooks::{use_async_with_options, UseAsyncOptions}; +use yew_router::hooks::use_navigator; + +use crate::Route; #[function_component] pub fn Nav() -> Html { + let navigator = use_navigator(); + let logged_in = use_async_with_options( + async move { + let location = web_sys::window().unwrap().location(); + let logged_in_url = format!( + "{}//{}/api/logged_in", + location.protocol().unwrap(), + location.host().unwrap() + ); + let resp = Request::get(&logged_in_url) + .send() + .await + .unwrap() + .json::() + .await + .unwrap(); + Result::::Ok(resp.logged_in) + }, + UseAsyncOptions::enable_auto(), + ); + let logout = { + let logged_in = logged_in.clone(); + Callback::from(move |_| { + let navigator = navigator.clone(); + let logged_in = logged_in.clone(); + //let update = update.clone(); + spawn_local(async move { + let location = web_sys::window().unwrap().location(); + let logout_url = format!( + "{}//{}/api/logout", + location.protocol().unwrap(), + location.host().unwrap() + ); + Request::post(&logout_url) + .body("") + .unwrap() + .send() + .await + .unwrap(); + logged_in.run(); + navigator.as_ref().unwrap().push(&Route::Home); + }); + }) + }; html! {