Skip to content

Commit

Permalink
Makes login app check refresh token on load
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbrain-za committed Nov 5, 2023
1 parent e4513f9 commit 92f05db
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "challenge_frontend"
version = "0.5.6"
version = "0.5.7"
edition = "2021"
rust-version = "1.71"
authors = ["Philip Barlow"]
Expand Down
15 changes: 8 additions & 7 deletions src/apps/login_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ pub struct LoginApp {

impl Default for LoginApp {
fn default() -> Self {
let url = option_env!("BACKEND_URL").unwrap().to_string();

Check failure on line 92 in src/apps/login_app.rs

View workflow job for this annotation

GitHub Actions / Clippy

this will panic at run-time if the environment variable doesn't exist at compile-time

Check failure on line 92 in src/apps/login_app.rs

View workflow job for this annotation

GitHub Actions / Clippy

this will panic at run-time if the environment variable doesn't exist at compile-time
Self {
token_refresh_promise: refresh::submit_refresh(&url),
login_promise: Default::default(),
register_promise: Default::default(),
url: option_env!("BACKEND_URL")
.unwrap_or("http://123.4.5.6:3000/api/auth")
.to_string(),
url,
login: LoginSchema {
email: "[email protected]".to_string(),
password: "password123".to_string(),
Expand All @@ -105,7 +105,6 @@ impl Default for LoginApp {
state: LoginState::LoggedOut,
register: RegisterSchema::default(),
toasts: Toasts::default(),
token_refresh_promise: None,
}
}
}
Expand Down Expand Up @@ -223,6 +222,7 @@ impl LoginApp {
Ok(LoginState::Expired) => {
self.state = LoginState::Expired;
self.token_refresh_promise = refresh::submit_refresh(&self.url);
self.submit = Some(AuthRequest::Logout);
}
Err(e) => {
self.toasts
Expand Down Expand Up @@ -379,8 +379,10 @@ impl super::App for LoginApp {
self.submit_login(ctx);
}
AuthRequest::Logout => {
log::debug!("Submitting logout request");
self.submit_logout(ctx);
if self.register_promise.is_none() {
log::debug!("Submitting logout request");
self.submit_logout(ctx);
}
}
AuthRequest::Register => {
log::debug!("Submitting register request");
Expand All @@ -400,7 +402,6 @@ impl super::App for LoginApp {
refresh::RefreshStatus::InProgress => {}
refresh::RefreshStatus::Success => {
self.state = LoginState::LoggedIn(self.login.email.clone());
self.submit = Some(AuthRequest::Logout);
}
refresh::RefreshStatus::Failed(_) => {
self.state = LoginState::LoggedOut;
Expand Down
7 changes: 2 additions & 5 deletions src/helpers/refresh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,10 @@ pub fn submit_refresh(url: &str) -> RefreshPromise {
.send()
.await
.unwrap();
let result = response
response
.json::<RefreshResponse>()
.await
.map_err(|e| e.to_string());
log::info!("Result: {:?}", result);

result
.map_err(|e| e.to_string())
}))
}

Expand Down

0 comments on commit 92f05db

Please sign in to comment.