From 72f3d8c5ab3e2a42cf1a76d0c08815dbe46e50a1 Mon Sep 17 00:00:00 2001 From: Owen Jacobson Date: Fri, 11 Oct 2024 01:18:18 -0400 Subject: Move login to its own route. This - in passing - fixes the problem where the client failed to subscribe after logging in, by causing the whole subscription process to be re-run when returning to the main interface. --- src/ui.rs | 46 +++++++++++++++++++++++++---------- ui/lib/apiServer.js | 2 ++ ui/lib/components/LogIn.svelte | 4 ++- ui/lib/components/LogOut.svelte | 8 ++++-- ui/lib/components/MessageInput.svelte | 2 +- ui/routes/(app)/+layout.svelte | 24 +++++++++--------- ui/routes/(login)/login/+page.svelte | 5 ++++ 7 files changed, 63 insertions(+), 28 deletions(-) create mode 100644 ui/routes/(login)/login/+page.svelte diff --git a/src/ui.rs b/src/ui.rs index b296325..34eb6f1 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -14,25 +14,45 @@ use crate::{app::App, channel, error::Internal, login::Login}; #[folder = "target/ui"] struct Assets; +impl Assets { + fn load(path: impl AsRef) -> Result> { + let path = path.as_ref(); + let mime = mime_guess::from_path(path).first_or_octet_stream(); + + Self::get(path) + .map(|file| Asset(mime, file)) + .ok_or(NotFound(format!("not found: {path}"))) + } + + fn index() -> Result { + // "not found" in this case really is an internal error, as it should + // never happen. `index.html` is a known-valid path. + Ok(Self::load("index.html")?) + } +} + pub fn router() -> Router { Router::new() .route("/*path", get(asset)) .route("/", get(root)) + .route("/login", get(login)) .route("/ch/:channel", get(channel)) } async fn asset(Path(path): Path) -> Result> { - let mime = mime_guess::from_path(&path).first_or_octet_stream(); + Assets::load(path) +} - Assets::get(&path) - .map(|file| Asset(mime, file)) - .ok_or(NotFound(format!("not found: {path}"))) +async fn root(login: Option) -> Result { + if login.is_none() { + Ok(Redirect::temporary("/login").into_response()) + } else { + Ok(Assets::index()?.into_response()) + } } -async fn root() -> Result { - // "not found" in this case really is an internal error, as it should - // never happen. `index.html` is a known-valid path. - Ok(asset(Path(String::from("index.html"))).await?) +async fn login() -> Result { + Assets::index() } async fn channel( @@ -40,13 +60,13 @@ async fn channel( login: Option, Path(channel): Path, ) -> Result { - Ok(if login.is_none() { - Redirect::temporary("/").into_response() + if login.is_none() { + Ok(Redirect::temporary("/").into_response()) } else if app.channels().get(&channel).await?.is_none() { - NotFound(root().await?).into_response() + Ok(NotFound(Assets::index()?).into_response()) } else { - root().await?.into_response() - }) + Ok(Assets::index()?.into_response()) + } } struct Asset(Mime, EmbeddedFile); diff --git a/ui/lib/apiServer.js b/ui/lib/apiServer.js index 538fa85..ccd6e66 100644 --- a/ui/lib/apiServer.js +++ b/ui/lib/apiServer.js @@ -66,6 +66,8 @@ export function subscribeToEvents(resume_point) { break; } } + + return evtSource; } function onLoginEvent(data) { diff --git a/ui/lib/components/LogIn.svelte b/ui/lib/components/LogIn.svelte index 2836e6d..e1cda8a 100644 --- a/ui/lib/components/LogIn.svelte +++ b/ui/lib/components/LogIn.svelte @@ -1,4 +1,5 @@
- @{$currentUser.username} + {#if $currentUser} + @{$currentUser.username} + {/if}