Skip to content

Commit

Permalink
Refactor login functionality into separate module and add login trans…
Browse files Browse the repository at this point in the history
…lation; remove unused imports
  • Loading branch information
nullchinchilla committed May 10, 2024
1 parent 197c5da commit 588d796
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 30 deletions.
1 change: 0 additions & 1 deletion binaries/geph5-broker/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use argon2::{password_hash::Encoding, Argon2, PasswordHash, PasswordVerifier};
use geph5_broker_protocol::{AccountLevel, AuthError};

use rand::Rng as _;
use tracing::Level;

use crate::{database::POSTGRES, log_error};

Expand Down
4 changes: 2 additions & 2 deletions binaries/geph5-client-gui/src/l10n.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

label,en,zh,ru,fa
about,About,关于,О программе,Darbāre
account_info,Account Info,帐户信息,Информация об аккаунте,Eṭṭelā'āt-e ḥesāb
Expand Down Expand Up @@ -69,6 +68,7 @@ help,Help,帮助,Помощь,Rāhnamā
language,Language,语言,Язык,Zabān
loading,Loading,加载中,Загрузка,Dar hāl-e bārgozārī
loading_exit_list,Loading exit list...,正在加载出口列表...,Загрузка списка выходов...,Dar hāl-e bārgozārī-ye liste xuruji-hā
login,Login,登录,Вход,Vorūd
logs,Logs,日志,Журналы,Lāg-hā
network_settings,Network Settings,网络设置,Настройки сети,Tanzimāt-e šabake
password,Password,密码,Пароль,Ramz-e 'obur
Expand All @@ -82,4 +82,4 @@ settings,Settings,设置,Настройки,Tanzimāt
status,Status,状态,Статус,Vazīyat
upload_speed,Upload Speed,上传速度,Скорость отдачи,Sor'at-e āplod
username,Username,用户名,Имя пользователя,Nām-e karbarī
zoom_factor,Zoom Factor,缩放,Масштабирование,Zarīb-e bozorg-namā'ī
zoom_factor,Zoom Factor,缩放,Масштабирование,Zarīb-e bozorg-namā'ī
21 changes: 4 additions & 17 deletions binaries/geph5-client-gui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use native_dialog::MessageType;
use once_cell::sync::Lazy;
use prefs::{pref_read, pref_write};
use settings::{USERNAME, ZOOM_FACTOR};
use tabs::{dashboard::Dashboard, logs::Logs, settings::render_settings};
use tabs::{dashboard::Dashboard, login::Login, logs::Logs, settings::render_settings};
use tracing_subscriber::{layer::SubscriberExt as _, util::SubscriberInitExt, EnvFilter};

use crate::{settings::PASSWORD, store_cell::StoreCell};
Expand Down Expand Up @@ -85,6 +85,7 @@ enum TabName {

pub struct App {
selected_tab: TabName,
login: Login,

dashboard: Dashboard,
logs: Logs,
Expand Down Expand Up @@ -123,6 +124,7 @@ impl App {

Self {
selected_tab: TabName::Dashboard,
login: Login::new(),

dashboard: Dashboard::new(),
logs: Logs::new(),
Expand All @@ -137,22 +139,7 @@ impl eframe::App for App {

if USERNAME.get().is_empty() {
egui::CentralPanel::default().show(ctx, |ui| {
static TMP_UNAME: Lazy<StoreCell<String>> =
Lazy::new(|| StoreCell::new("".to_string()));
static TMP_PWD: Lazy<StoreCell<String>> =
Lazy::new(|| StoreCell::new("".to_string()));

ui.label(l10n("username"));
TMP_UNAME.modify(|username| ui.text_edit_singleline(username));

ui.label(l10n("password"));
TMP_PWD.modify(|pwd| ui.add(egui::TextEdit::singleline(pwd).password(true)));

if ui.button(l10n("save")).clicked() {
// TODO verify
USERNAME.set(TMP_UNAME.get().clone());
PASSWORD.set(TMP_PWD.get().clone());
}
self.login.render(ui).unwrap();
});

return;
Expand Down
13 changes: 3 additions & 10 deletions binaries/geph5-client-gui/src/settings.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
use std::time::Duration;

use egui::mutex::Mutex;
use geph5_broker_protocol::{BrokerClient, Credential, ExitList};
use geph5_broker_protocol::Credential;
use geph5_client::{Config, ExitConstraint};
use isocountry::CountryCode;
use itertools::Itertools;

use once_cell::sync::Lazy;
use smol_str::{SmolStr, ToSmolStr};

use crate::{
l10n::{l10n, l10n_country},
refresh_cell::RefreshCell,
store_cell::StoreCell,
};
use crate::store_cell::StoreCell;

pub fn get_config() -> anyhow::Result<Config> {
let yaml: serde_yaml::Value = serde_yaml::from_str(include_str!("settings_default.yaml"))?;
Expand Down
74 changes: 74 additions & 0 deletions binaries/geph5-client-gui/src/tabs/login.rs
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(())
}
1 change: 1 addition & 0 deletions binaries/geph5-client-gui/src/tabs/mod.rs
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;

0 comments on commit 588d796

Please sign in to comment.