Skip to content

Commit

Permalink
Refactor login function and add error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
rushi3691 committed Mar 27, 2024
1 parent 1c803e6 commit e140ede
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src-tauri/src/fortinet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub async fn login(

// let session_id = keep_alive_url.split('?').collect::<Vec<&str>>()[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(())
}
Expand Down
7 changes: 5 additions & 2 deletions src-tauri/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, ()> {
) -> Result<String, String> {
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();
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -24,6 +23,7 @@ pub async fn worker(
sleep(Duration::from_secs(1)).await;
continue;
}
Ok(_) => {}
}

sleep(Duration::from_secs(SLEEP_TIME)).await;
Expand Down
6 changes: 5 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down

0 comments on commit e140ede

Please sign in to comment.