Skip to content

Commit

Permalink
Refresh login state on load
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbrain-za committed Nov 12, 2023
1 parent f2cc4d0 commit 9743332
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
15 changes: 8 additions & 7 deletions src/apps/login_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,8 @@ impl LoginApp {
self.toasts
.info(format!("Logged in: {}", &self.login.email))
.set_duration(Some(Duration::from_secs(5)));
let app = Arc::clone(&self.app_state);
let mut app = app.lock().unwrap();
app.logged_in = LoginState::LoggedIn(self.login.email.clone());

AppState::set_logged_in(&self.app_state);
}
LoginResponse::Failure { status: _, message } => {
log::error!("Failed to login: {}", message);
Expand Down Expand Up @@ -224,9 +223,7 @@ impl LoginApp {
self.toasts
.info(format!("Logged out: {}", &self.login.email))
.set_duration(Some(Duration::from_secs(5)));
let app = Arc::clone(&self.app_state);
let mut app = app.lock().unwrap();
app.logged_in = LoginState::LoggedOut;
AppState::set_logged_out(&self.app_state);
self.logout_requestor = None;
}
_ => {}
Expand Down Expand Up @@ -438,6 +435,10 @@ impl super::App for LoginApp {

fn set_app_state_ref(&mut self, app_state: Arc<Mutex<AppState>>) {
self.app_state = app_state;
let app_state = Arc::clone(&self.app_state);
let mut req = Requestor::new_refresh(app_state);
req.send();
self.login_requestor = Some(req);
}

fn show(&mut self, ctx: &egui::Context, open: &mut bool) {
Expand All @@ -464,7 +465,7 @@ impl super::View for LoginApp {

match self.state {
LoginAppState::Login => match logged_in {
LoginState::LoggedIn(_) => self.ui_logged_in(ui),
LoginState::LoggedIn => self.ui_logged_in(ui),
LoginState::LoggedOut => self.ui_logged_out(ui),
},
LoginAppState::RegisterNewUser => self.ui_register(ui),
Expand Down
17 changes: 16 additions & 1 deletion src/helpers/app_state.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::sync::{Arc, Mutex};

#[derive(Clone)]
pub enum LoginState {
LoggedIn(String),
LoggedIn,
LoggedOut,
}

Expand All @@ -17,3 +19,16 @@ impl Default for AppState {
}
}
}

impl AppState {
pub fn set_logged_in(app_state: &Arc<Mutex<AppState>>) {
let app = Arc::clone(app_state);
let mut app = app.lock().unwrap();
app.logged_in = LoginState::LoggedIn;
}
pub fn set_logged_out(app_state: &Arc<Mutex<AppState>>) {
let app = Arc::clone(app_state);
let mut app = app.lock().unwrap();
app.logged_in = LoginState::LoggedOut;
}
}
20 changes: 16 additions & 4 deletions src/helpers/fetchers.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::helpers::{refresh, AppState, LoginState};
use crate::helpers::{refresh, AppState};
use gloo_net::http;
use poll_promise::Promise;
use std::sync::{Arc, Mutex};
Expand All @@ -15,6 +15,7 @@ pub enum RequestStatus {
enum Method {
Get,
Post,
Refresh,
}

impl std::fmt::Display for RequestStatus {
Expand Down Expand Up @@ -52,6 +53,12 @@ impl Requestor {
Self::new(app_state, url, with_credentials, None, None, Method::Get)
}

pub fn new_refresh(app_state: Arc<Mutex<AppState>>) -> Self {
let mut s = Self::new(app_state, "", true, None, None, Method::Refresh);
s.refresh_login();
s
}

pub fn new_post(
app_state: Arc<Mutex<AppState>>,
url: &str,
Expand Down Expand Up @@ -100,6 +107,8 @@ impl Requestor {
refresh::RefreshStatus::InProgress => {}
refresh::RefreshStatus::Success => {
log::debug!("Retrying Request");
AppState::set_logged_in(&self.app_state);
self.state_has_changed = true;
self.send();
return RequestStatus::InProgress;
}
Expand All @@ -123,9 +132,7 @@ impl Requestor {
self.token_refresh_promise = refresh::submit_refresh();
RequestStatus::InProgress
} else {
let app = Arc::clone(&self.app_state);
let mut app = app.lock().unwrap();
app.logged_in = LoginState::LoggedOut;
AppState::set_logged_out(&self.app_state);
RequestStatus::Failed("Authentication failed".to_string())
}
}
Expand Down Expand Up @@ -155,9 +162,14 @@ impl Requestor {
match self.method {
Method::Get => self.get(),
Method::Post => self.post(),
Method::Refresh => (),
}
}

fn refresh_login(&mut self) {
self.token_refresh_promise = refresh::submit_refresh();
}

fn get(&mut self) {
let url = self.url.clone();
let with_credentials = self.with_credentials;
Expand Down

0 comments on commit 9743332

Please sign in to comment.