From ec66b58f2b728804c67391769c55a5fae317c97d Mon Sep 17 00:00:00 2001 From: dr-frmr Date: Mon, 4 Nov 2024 13:43:24 -0500 Subject: [PATCH] WIP --- kinode/src/http/login.html | 11 ----------- kinode/src/http/server.rs | 20 +++++++++++++++++++- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/kinode/src/http/login.html b/kinode/src/http/login.html index afcff2ef..680080c7 100644 --- a/kinode/src/http/login.html +++ b/kinode/src/http/login.html @@ -231,17 +231,6 @@ } }); }); - - // Check for redirect parameter on page load - document.addEventListener("DOMContentLoaded", () => { - const urlParams = new URLSearchParams(window.location.search); - const redirectPath = urlParams.get('redirect'); - if (redirectPath) { - // Ensure the redirect path starts with a slash - const path = redirectPath.startsWith('/') ? redirectPath : '/' + redirectPath; - window.location.href = path; - } - }); diff --git a/kinode/src/http/server.rs b/kinode/src/http/server.rs index 7481d979..6fc57368 100644 --- a/kinode/src/http/server.rs +++ b/kinode/src/http/server.rs @@ -294,6 +294,8 @@ async fn serve( .or(warp::post() .and(warp::body::content_length_limit(1024 * 16)) .and(warp::body::json()) + .and(warp::filters::host::optional()) + .and(warp::query::>()) .and(warp::any().map(move || cloned_our.clone())) .and(warp::any().map(move || encoded_keyfile.clone())) .and_then(login_handler)), @@ -324,7 +326,12 @@ async fn serve( /// handle non-GET requests on /login. if POST, validate password /// and return auth token, which will be stored in a cookie. +/// +/// if redirect is provided in URL, such as ?redirect=/chess:chess:sys/, +/// the browser will be redirected to that path after successful login. async fn login_handler( + host: Option, + query_params: HashMap, info: LoginInfo, our: Arc, encoded_keyfile: Arc>, @@ -354,7 +361,11 @@ async fn login_handler( let mut response = warp::reply::with_status( warp::reply::json(&base64_standard.encode(encoded_keyfile.to_vec())), - StatusCode::OK, + if let Some(redirect) = query_params.get("redirect") { + StatusCode::FOUND + } else { + StatusCode::OK + }, ) .into_response(); @@ -366,6 +377,13 @@ async fn login_handler( match HeaderValue::from_str(&cookie) { Ok(v) => { response.headers_mut().append("set-cookie", v); + if let Some(redirect) = query_params.get("redirect") { + response.headers_mut().append( + "Location", + HeaderValue::from_str(&format!("{}{redirect}", host.unwrap().host())) + .unwrap(), + ); + } Ok(response) } Err(e) => Ok(warp::reply::with_status(