Skip to content

Commit

Permalink
#129: clear saved credentials when login fails
Browse files Browse the repository at this point in the history
  • Loading branch information
xou816 committed Sep 22, 2021
1 parent 2a591fe commit dad8116
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/app/components/login/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ impl EventListener for Login {
self.hide_and_save_creds(creds.clone());
}
AppEvent::LoginEvent(LoginEvent::LoginFailed) => {
self.model.clear_saved_credentials();
self.reveal_error();
}
AppEvent::Started => {
Expand Down
4 changes: 4 additions & 0 deletions src/app/components/login/login_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ impl LoginModel {
}
}

pub fn clear_saved_credentials(&self) {
let _ = Credentials::logout();
}

pub fn save_token(&self, token: String, token_expiry_time: SystemTime) {
if let Ok(mut credentials) = Credentials::retrieve() {
credentials.token = token;
Expand Down
4 changes: 2 additions & 2 deletions src/app/components/user_menu/user_menu_model.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::api::clear_user_cache;
use crate::app::credentials;
use crate::app::credentials::Credentials;
use crate::app::models::{PlaylistDescription, PlaylistSummary};
use crate::app::state::{LoginAction, PlaybackAction};
use crate::app::{ActionDispatcher, AppModel};
Expand Down Expand Up @@ -27,7 +27,7 @@ impl UserMenuModel {
pub fn logout(&self) {
self.dispatcher.dispatch(PlaybackAction::Stop.into());
self.dispatcher.dispatch_async(Box::pin(async {
let _ = credentials::logout();
let _ = Credentials::logout();
let _ = clear_user_cache().await;
Some(LoginAction::Logout.into())
}));
Expand Down
17 changes: 8 additions & 9 deletions src/app/credentials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ impl Credentials {
serde_json::from_slice(&item).map_err(|_| Error::Parse)
}

pub fn logout() -> Result<(), Error> {
let service = SecretService::new(EncryptionType::Dh)?;
let collection = service.get_default_collection()?;
let result = collection.search_items(make_attributes())?;
let item = result.get(0).ok_or(Error::NoResult)?;
item.delete()
}

pub fn save(&self) -> Result<(), Error> {
let service = SecretService::new(EncryptionType::Dh)?;
let collection = service.get_default_collection()?;
Expand All @@ -49,12 +57,3 @@ impl Credentials {
Ok(())
}
}

pub fn logout() -> Result<(), Error> {
let service = SecretService::new(EncryptionType::Dh)?;
let collection = service.get_default_collection()?;
let result = collection.search_items(make_attributes())?;

let item = result.get(0).ok_or(Error::NoResult)?;
item.delete()
}

0 comments on commit dad8116

Please sign in to comment.