From 269753dd806274c8da103ebf23bb6cebccbf4486 Mon Sep 17 00:00:00 2001 From: h0lybyte <5599058+h0lybyte@users.noreply.github.com> Date: Wed, 11 Jun 2025 22:36:48 -0400 Subject: [PATCH 01/14] fix: cleaning up the auth. --- index.html | 23 ++++++++++- src/app.rs | 16 ++++++++ src/erust/uiux/javascript_interop.rs | 59 ++++++++++++++++++++++++---- 3 files changed, 89 insertions(+), 9 deletions(-) diff --git a/index.html b/index.html index 707d284..3363c6c 100644 --- a/index.html +++ b/index.html @@ -34,7 +34,12 @@ inject: ` const supabase = $module.createClient( "https://qmpdruitzlownnnnjmpk.supabase.co", - "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InFtcGRydWl0emxvd25ubm5qbXBrIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NDk2NjA0NTYsImV4cCI6MjA2NTIzNjQ1Nn0.OhD3qN4dq0TMA65qVGvry_QsZEeLKK7RbwYP3QzAvcY" + "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InFtcGRydWl0emxvd25ubm5qbXBrIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NDk2NjA0NTYsImV4cCI6MjA2NTIzNjQ1Nn0.OhD3qN4dq0TMA65qVGvry_QsZEeLKK7RbwYP3QzAvcY", + { auth: { + detectSessionInUrl: false, + persistSession: false, + } + } ); self.onmessage = async (e) => { @@ -59,10 +64,13 @@ password: payload.password, options: { captchaToken: payload.captchaToken } }); + console.log("[Worker] Login result:", result); + break; case "logout": result = await supabase.auth.signOut(); + console.log("[Worker] Logout result:", result); break; case "session": @@ -414,6 +422,19 @@ }); } break; + case "logout": + if (window.supabase && window.supabase.logout) { + window.supabase.logout() + .then((resp) => { + if (window.JSRustResponseHandler) { + window.JSRustResponseHandler(resp); + } + }) + .catch((err) => { + console.error("[ERROR]: logout call failed", err); + }); + } + break; case "log": if (message) { console.log("[JSRust]", message); diff --git a/src/app.rs b/src/app.rs index d84b367..0d09d48 100644 --- a/src/app.rs +++ b/src/app.rs @@ -376,6 +376,22 @@ impl TemplateApp { ui.label("Please sync the languages you would like to see."); }); } + + // --- Always-visible bottom panel with Logout button --- + egui::TopBottomPanel::bottom("bottom_panel").show(ctx, |ui| { + ui.horizontal(|ui| { + if ui.button("Logout").clicked() { + crate::erust::uiux::javascript_interop::send_action_message( + "logout", + "", + "", + "", + ); + self.toast_message = Some("Sent logout request to JS".to_string()); + } + ui.label("Waffle v0.1.0"); + }); + }); } } diff --git a/src/erust/uiux/javascript_interop.rs b/src/erust/uiux/javascript_interop.rs index b4c69c4..66d591a 100644 --- a/src/erust/uiux/javascript_interop.rs +++ b/src/erust/uiux/javascript_interop.rs @@ -29,15 +29,31 @@ pub fn send_action_message(action: &str, email: &str, password: &str, captcha_to /// Register a JS callback handler for responses from JS to Rust /// The callback will be called with a JsValue (the response object) use wasm_bindgen::prelude::*; +use wasm_bindgen::JsCast; +use js_sys::JSON; -#[wasm_bindgen] -pub fn set_jsrust_response_handler(cb: &js_sys::Function) { - // Store the callback globally in JS (window.JSRustResponseHandler) - let _ = js_sys::Reflect::set( - &js_sys::global(), - &JsValue::from_str("JSRustResponseHandler"), - cb, - ); +/// Call this during app initialization to handle JS->Rust responses +pub fn setup_jsrust_response_handler(mut callback: F) +where + F: 'static + FnMut(serde_json::Value), +{ + let handler = Closure::wrap(Box::new(move |resp: JsValue| { + // Convert JsValue to serde_json::Value using JSON::stringify and serde_json + let resp_json = if let Ok(js_str) = JSON::stringify(&resp) { + if let Some(s) = js_str.as_string() { + serde_json::from_str(&s).unwrap_or_default() + } else { + serde_json::Value::Null + } + } else { + serde_json::Value::Null + }; + callback(resp_json); + }) as Box); + + // Call the local set_jsrust_response_handler directly + set_jsrust_response_handler(handler.as_ref().unchecked_ref()); + handler.forget(); // Prevent the closure from being dropped } /// Call this from JS to send a response back to Rust @@ -51,3 +67,30 @@ pub fn handle_jsrust_response(response: &JsValue) { } } } + +// Add back the set_jsrust_response_handler as a public function so it is available in scope +#[wasm_bindgen] +pub fn set_jsrust_response_handler(cb: &js_sys::Function) { + // Store the callback globally in JS (window.JSRustResponseHandler) + let _ = js_sys::Reflect::set( + &js_sys::global(), + &JsValue::from_str("JSRustResponseHandler"), + cb, + ); +} + +// Expand AppState for interop waiting +// In state.rs (or wherever your AppState is defined): +// +// pub enum AppState { +// Init, +// Normal, +// Empty, +// Error(String), +// InteropPending(String), // Add this variant for JS interop waiting +// } +// +// Usage example: +// self.app_state = AppState::InteropPending("Waiting for JS response...".to_string()); +// +// In your interop callback, set it back to Normal or Error as appropriate. From 520dcaabd4e8150f4a17079f46e879cda3ea9529 Mon Sep 17 00:00:00 2001 From: h0lybyte <5599058+h0lybyte@users.noreply.github.com> Date: Thu, 12 Jun 2025 00:43:08 -0400 Subject: [PATCH 02/14] fix: register and login work! yay. --- index.html | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 97 insertions(+), 5 deletions(-) diff --git a/index.html b/index.html index 3363c6c..1bb0a05 100644 --- a/index.html +++ b/index.html @@ -9,7 +9,6 @@ /> - Waffle - Template @@ -26,6 +25,43 @@ From 0f346caff9a309cd366cbcbb2cf9c286b3bf1e2e Mon Sep 17 00:00:00 2001 From: h0lybyte <5599058+h0lybyte@users.noreply.github.com> Date: Thu, 12 Jun 2025 02:07:45 -0400 Subject: [PATCH 07/14] fix: updating anon key. --- index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.html b/index.html index ba44f03..07810ae 100644 --- a/index.html +++ b/index.html @@ -70,7 +70,7 @@ inject: ` const supabase = $module.createClient( "https://qmpdruitzlownnnnjmpk.supabase.co", - "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InFtcGRydWl0emxvd25ubm5qbXBrIiwicm9zZSI6ImFub24iLCJpYXQiOjE3NDk2NjA0NTYsImV4cCI6MjA2NTIzNjQ1Nn0.OhD3qN4dq0TMA65qVGvry_QsZEeLKK7RbwYP3QzAvcY", + "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InFtcGRydWl0emxvd25ubm5qbXBrIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NDk2NjA0NTYsImV4cCI6MjA2NTIzNjQ1Nn0", { auth: { detectSessionInUrl: false, From 5f720dbdf4f020ea2ca0b58a013e22d89899d0a8 Mon Sep 17 00:00:00 2001 From: h0lybyte <5599058+h0lybyte@users.noreply.github.com> Date: Thu, 12 Jun 2025 02:47:43 -0400 Subject: [PATCH 08/14] fix: keys. --- index.html | 21 ++++++++++++------ src/erust/uiux/javascript_interop.rs | 32 ++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/index.html b/index.html index 07810ae..affcce3 100644 --- a/index.html +++ b/index.html @@ -54,12 +54,19 @@ refresh_token: session.refresh_token, }, }); + + let retries = 50; + while (!window.supabase?.worker && retries-- > 0) { + await new Promise((r) => setTimeout(r, 100)); + } + + if (window.supabase?.worker) { + } } else { console.log("[Main] Worker not found."); } - } - else { - console.log("[Main] No session found — user is logged out."); + } else { + console.log("[Main] No session found — user is logged out."); } })(); @@ -70,7 +77,7 @@ inject: ` const supabase = $module.createClient( "https://qmpdruitzlownnnnjmpk.supabase.co", - "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InFtcGRydWl0emxvd25ubm5qbXBrIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NDk2NjA0NTYsImV4cCI6MjA2NTIzNjQ1Nn0", + "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InFtcGRydWl0emxvd25ubm5qbXBrIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NDk2NjA0NTYsImV4cCI6MjA2NTIzNjQ1Nn0.OhD3qN4dq0TMA65qVGvry_QsZEeLKK7RbwYP3QzAvcY", { auth: { detectSessionInUrl: false, @@ -186,7 +193,7 @@ session: () => sendToWorker("session", {}), getUser: () => sendToWorker("getUser", {}), getProfile: (user_id) => sendToWorker("getProfile", { user_id }), - setSession: () => sendToWorker("setSession", {session}), + setSession: () => sendToWorker("setSession", { session }), worker: supabaseWorker, }; @@ -379,14 +386,14 @@ >