Skip to content

Commit 95e5494

Browse files
authored
Fix clearing credentials (#12041)
### Related * Closes RR-3070 ### What Makes clearing the credentials actually remove the saved credentials
1 parent 72a788d commit 95e5494

File tree

3 files changed

+62
-6
lines changed

3 files changed

+62
-6
lines changed

crates/utils/re_auth/src/oauth.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ pub fn load_credentials() -> Result<Option<Credentials>, CredentialsLoadError> {
4848
}
4949
}
5050

51+
#[derive(Debug, thiserror::Error)]
52+
#[error("failed to load credentials: {0}")]
53+
pub struct CredentialsClearError(#[from] storage::ClearError);
54+
55+
pub fn clear_credentials() -> Result<(), CredentialsClearError> {
56+
storage::clear()?;
57+
58+
Ok(())
59+
}
60+
5161
#[derive(Debug, thiserror::Error)]
5262
pub enum CredentialsRefreshError {
5363
#[error("failed to refresh credentials: {0}")]

crates/utils/re_auth/src/oauth/storage.rs

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,29 @@ pub enum StoreError {
3636
NoLocalStorage,
3737
}
3838

39+
#[derive(Debug, thiserror::Error)]
40+
pub enum ClearError {
41+
#[error("failed to clear credentials: {0}")]
42+
Io(#[from] std::io::Error),
43+
44+
#[cfg(not(target_arch = "wasm32"))]
45+
#[error("could not find a valid config location, please ensure $HOME is set")]
46+
UnknownConfigLocation,
47+
48+
#[cfg(target_arch = "wasm32")]
49+
#[error("failed to get window.localStorage")]
50+
NoLocalStorage,
51+
}
52+
3953
#[cfg(not(target_arch = "wasm32"))]
40-
pub use file::{load, store};
54+
pub use file::{clear, load, store};
4155

4256
#[cfg(target_arch = "wasm32")]
43-
pub use web::{load, store};
57+
pub use web::{clear, load, store};
4458

4559
#[cfg(not(target_arch = "wasm32"))]
4660
mod file {
47-
use super::{Credentials, LoadError, StoreError};
61+
use super::{ClearError, Credentials, LoadError, StoreError};
4862
use std::path::PathBuf;
4963

5064
fn credentials_path() -> Option<PathBuf> {
@@ -71,13 +85,24 @@ mod file {
7185
std::fs::write(path, data)?;
7286
Ok(())
7387
}
88+
89+
pub fn clear() -> Result<(), ClearError> {
90+
let path = credentials_path().ok_or(ClearError::UnknownConfigLocation)?;
91+
92+
match std::fs::remove_file(path) {
93+
Ok(()) => Ok(()),
94+
// If the file didn't exist this isn't a failure.
95+
Err(err) if err.kind() == std::io::ErrorKind::NotFound => Ok(()),
96+
Err(err) => Err(ClearError::Io(err)),
97+
}
98+
}
7499
}
75100

76101
#[cfg(target_arch = "wasm32")]
77102
mod web {
78103
use wasm_bindgen::JsCast as _;
79104

80-
use super::{Credentials, LoadError, StoreError};
105+
use super::{ClearError, Credentials, LoadError, StoreError};
81106

82107
const STORAGE_KEY: &str = "rerun_auth";
83108

@@ -95,6 +120,12 @@ mod web {
95120
}
96121
}
97122

123+
impl From<NoLocalStorage> for ClearError {
124+
fn from(_: NoLocalStorage) -> Self {
125+
Self::NoLocalStorage
126+
}
127+
}
128+
98129
#[expect(clippy::needless_pass_by_value)]
99130
pub fn string_from_js_value(s: wasm_bindgen::JsValue) -> String {
100131
// it's already a string
@@ -140,4 +171,12 @@ mod web {
140171
.map_err(|err| std::io::Error::other(string_from_js_value(err)))?;
141172
Ok(())
142173
}
174+
175+
pub fn clear() -> Result<(), ClearError> {
176+
let local_storage = get_local_storage()?;
177+
local_storage
178+
.remove_item(STORAGE_KEY)
179+
.map_err(|err| std::io::Error::other(string_from_js_value(err)))?;
180+
Ok(())
181+
}
143182
}

crates/viewer/re_redap_browser/src/server_modal.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,15 @@ fn auth_ui(ui: &mut egui::Ui, cmd: &CommandSender, auth: &mut Authentication) {
369369
.on_hover_text("Clear login status")
370370
.clicked()
371371
{
372-
auth.error = None;
373-
auth.start_login_flow(ui);
372+
match re_auth::oauth::clear_credentials() {
373+
Ok(()) => {
374+
auth.email = None;
375+
auth.start_login_flow(ui);
376+
}
377+
Err(err) => {
378+
auth.error = Some(err.to_string());
379+
}
380+
}
374381
}
375382
} else if auth.error.is_some() {
376383
if ui

0 commit comments

Comments
 (0)