-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor login functionality into separate module and add login trans…
…lation; remove unused imports
- Loading branch information
1 parent
197c5da
commit 588d796
Showing
6 changed files
with
84 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use std::time::Duration; | ||
|
||
use geph5_broker_protocol::{BrokerClient, Credential}; | ||
use poll_promise::Promise; | ||
|
||
use crate::{ | ||
l10n::l10n, | ||
settings::{get_config, PASSWORD, USERNAME}, | ||
}; | ||
|
||
pub struct Login { | ||
username: String, | ||
password: String, | ||
|
||
check_login: Option<Promise<anyhow::Result<()>>>, | ||
} | ||
|
||
impl Login { | ||
pub fn new() -> Self { | ||
Self { | ||
username: "".to_string(), | ||
password: "".to_string(), | ||
|
||
check_login: None, | ||
} | ||
} | ||
|
||
pub fn render(&mut self, ui: &mut egui::Ui) -> anyhow::Result<()> { | ||
if let Some(promise) = self.check_login.as_ref() { | ||
match promise.poll() { | ||
std::task::Poll::Ready(ready) => match ready { | ||
Ok(_) => { | ||
USERNAME.set(self.username.clone()); | ||
PASSWORD.set(self.password.clone()); | ||
} | ||
Err(err) => { | ||
ui.centered_and_justified(|ui| { | ||
ui.colored_label(egui::Color32::DARK_RED, format!("{:?}", err)) | ||
}); | ||
} | ||
}, | ||
std::task::Poll::Pending => { | ||
ui.centered_and_justified(|ui| { | ||
ui.spinner(); | ||
}); | ||
} | ||
} | ||
} else { | ||
ui.label(l10n("username")); | ||
ui.text_edit_singleline(&mut self.username); | ||
|
||
ui.label(l10n("password")); | ||
ui.text_edit_singleline(&mut self.password); | ||
|
||
if ui.button(l10n("login")).clicked() { | ||
let username = self.username.clone(); | ||
let password = self.password.clone(); | ||
self.check_login = Some(Promise::spawn_thread("check_login", move || { | ||
smol::future::block_on(check_login(username, password)) | ||
})); | ||
} | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
async fn check_login(username: String, password: String) -> anyhow::Result<()> { | ||
let mut config = get_config()?; | ||
config.credentials = Credential::LegacyUsernamePassword { username, password }; | ||
let rpc_transport = config.broker.unwrap().rpc_transport(); | ||
let client = BrokerClient::from(rpc_transport); | ||
client.get_auth_token(config.credentials.clone()).await??; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod dashboard; | ||
pub mod login; | ||
pub mod logs; | ||
pub mod settings; |