Browse Source

probably the last change until i move to nextjs

Leo dev 2 weeks ago
parent
commit
153d35825d
6 changed files with 61 additions and 9 deletions
  1. 1 2
      assets/styles/dashboard.css
  2. 5 0
      assets/styles/server.css
  3. 5 0
      src/backend/auth.rs
  4. 5 5
      src/dashboard.rs
  5. 22 1
      src/login.rs
  6. 23 1
      src/utils.rs

+ 1 - 2
assets/styles/dashboard.css

@@ -251,8 +251,7 @@ body {
 }
 
 .search-container button {
-  background-color: transparent;
-  border: 1px solid currentColor;
+  border: 0;
   border-radius: 6px;
   outline: none;
   white-space: nowrap;

+ 5 - 0
assets/styles/server.css

@@ -114,6 +114,11 @@
   gap: 6px;
 }
 
+.All {
+  color: var(--fg);
+  background-color: var(--bg);
+}
+
 .Online {
   color: var(--online);
   background-color: var(--online-bg);

+ 5 - 0
src/backend/auth.rs

@@ -62,3 +62,8 @@ pub async fn validate_session(id: String) -> bool {
         false
     }
 }
+
+#[server]
+pub async fn check_validate_session(id: String) -> Result<bool, ServerFnError> {
+    Ok(validate_session(id).await)
+}

+ 5 - 5
src/dashboard.rs

@@ -98,11 +98,11 @@ pub fn Dashboard() -> Element {
 
                             placeholder: "Search servers by name, location, or IP..." }
                     }
-                    button { class: "fg", "All (4)" }
-                    button { class: "online", "Online (3)" }
-                    button { class: "issues", "Warning (1)" }
-                    button { class: "ram", "Offline (1)" }
-                    button { class: "cpu", "Maintenance (1)" }
+                    button { class: "All", "All (4)" }
+                    button { class: "Online", "Online (3)" }
+                    button { class: "Warning", "Warning (1)" }
+                    button { class: "Offline", "Offline (1)" }
+                    button { class: "Maintenance", "Maintenance (1)" }
                 }
 
                 div { class: "server-container",

+ 22 - 1
src/login.rs

@@ -1,6 +1,9 @@
 use dioxus::prelude::*;
 
-use crate::{backend, utils};
+use crate::{
+    backend::{self},
+    utils::{self},
+};
 
 const LOGIN_CSS: Asset = asset!("/assets/styles/login.css");
 
@@ -9,6 +12,24 @@ pub fn Login() -> Element {
     let username = use_signal(|| String::new());
     let password = use_signal(|| String::new());
     let error: Signal<Option<String>> = use_signal(|| None);
+    let session = utils::use_session();
+
+    // Check for existing session on mount
+    use_effect({
+        let session = session.clone();
+        move || {
+            let session = session.clone();
+            spawn(async move {
+                // Optional: verify with the server if session is actually valid
+                if backend::auth::check_validate_session(session.get_id())
+                    .await
+                    .unwrap_or(false)
+                {
+                    utils::redirect("/dashboard");
+                }
+            });
+        }
+    });
 
     let is_disabled = {
         let username = username.clone();

+ 23 - 1
src/utils.rs

@@ -66,6 +66,7 @@ pub fn redirect(target: &str) {
         .unwrap();
 }
 
+#[derive(Clone)]
 pub struct Session(Signal<String>);
 
 pub fn use_session() -> Session {
@@ -73,6 +74,10 @@ pub fn use_session() -> Session {
 }
 
 impl Session {
+    pub fn get_id(&self) -> String {
+        self.0.read().clone()
+    }
+
     pub fn use_servers(&self) -> (Signal<Vec<Server>>, Signal<String>) {
         let query = use_signal(|| String::new());
         let servers = use_signal(|| Vec::new());
@@ -83,7 +88,7 @@ impl Session {
             let mut servers = servers.clone();
 
             spawn(async move {
-                if let Ok(v) = backend::get_servers(id.read().clone(), query_value).await {
+                if let Ok(v) = request(backend::get_servers(id.read().clone(), query_value).await) {
                     servers.set(v);
                 }
             });
@@ -92,3 +97,20 @@ impl Session {
         (servers, query)
     }
 }
+
+pub fn request<T>(o: Result<T, ServerFnError>) -> Result<T, ServerFnError> {
+    match o {
+        Err(e) => match e {
+            ServerFnError::ServerError(e) => {
+                if e == "Invalid session" {
+                    redirect("/");
+                    Err(ServerFnError::ServerError(e))
+                } else {
+                    Err(ServerFnError::ServerError(e))
+                }
+            }
+            _ => Err(e),
+        },
+        _ => o,
+    }
+}