From e140edeb1a48852596aa136efd2f09b3d70505a0 Mon Sep 17 00:00:00 2001 From: rushi3691 Date: Wed, 27 Mar 2024 16:02:16 +0530 Subject: [PATCH] Refactor login function and add error handling --- src-tauri/src/fortinet.rs | 2 +- src-tauri/src/handler.rs | 7 +++++-- src-tauri/src/worker.rs | 2 +- src/main.js | 6 +++++- 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src-tauri/src/fortinet.rs b/src-tauri/src/fortinet.rs index 54b48bb..1c5a14d 100644 --- a/src-tauri/src/fortinet.rs +++ b/src-tauri/src/fortinet.rs @@ -67,7 +67,7 @@ pub async fn login( // let session_id = keep_alive_url.split('?').collect::>()[1]; let session_id = keep_alive_url.split("?").nth(1).ok_or("Session id not found")?; - println!("{}", session_id); + println!("Logged in with sid: {}", session_id); Ok(()) } diff --git a/src-tauri/src/handler.rs b/src-tauri/src/handler.rs index 159de84..efe6d79 100644 --- a/src-tauri/src/handler.rs +++ b/src-tauri/src/handler.rs @@ -2,16 +2,19 @@ use std::{fs::File, io::Write}; use tauri::Manager; -use crate::{worker::worker, Credentials}; +use crate::{fortinet, worker::worker, Credentials}; #[tauri::command] pub async fn store_credentials( app_handle: tauri::AppHandle, username: String, password: String, -) -> Result { +) -> Result { let s = format!("{}\n{}", username, password); println!("{}", s); + + // check credentials valid or not, by logging in + fortinet::login(&username, &password).await.or(Err("Login failed"))?; // write to file let app_data_path = app_handle.path_resolver().app_data_dir().unwrap(); diff --git a/src-tauri/src/worker.rs b/src-tauri/src/worker.rs index 2b0da98..ddd2e07 100644 --- a/src-tauri/src/worker.rs +++ b/src-tauri/src/worker.rs @@ -14,7 +14,6 @@ pub async fn worker( let res = fortinet::login(&username, &password).await; match res { - Ok(_) => println!("Logged in!"), Err(e) => { println!("Error: {}", e); if tries == 1 { @@ -24,6 +23,7 @@ pub async fn worker( sleep(Duration::from_secs(1)).await; continue; } + Ok(_) => {} } sleep(Duration::from_secs(SLEEP_TIME)).await; diff --git a/src/main.js b/src/main.js index 856d0a4..64bb92c 100644 --- a/src/main.js +++ b/src/main.js @@ -6,7 +6,11 @@ let credsMsgEl; async function store_credentials() { console.log("creds", credsUsernameEl.value, credsPasswordEl.value) - credsMsgEl.textContent = await invoke("store_credentials", { username: credsUsernameEl.value, password: credsPasswordEl.value }); + try{ + credsMsgEl.textContent = await invoke("store_credentials", { username: credsUsernameEl.value, password: credsPasswordEl.value }); + }catch(e){ + credsMsgEl.textContent = e; + } } window.addEventListener("DOMContentLoaded", () => {