Commit 4baa623504

4baa623504c8991b739384e995dc69d6ac42f74f

parent: beb8e5265d

Unregistered key

Ashutosh Panda <133190480+Ashutosh-Panda2004@users.noreply.github.com> · 2026-10-04 23:27 UTC
committer: <noreply@github.com>

Reuse the old device ID when logging back in (#777)

Layout: unified · split

src/config/mod.rs +44 −1
@@ -14,7 +14,7 @@ use matrix_sdk::EncryptionState;
1414use matrix_sdk::authentication::matrix::MatrixSession;
1515use matrix_sdk::media::MediaRetentionPolicy;
1616use matrix_sdk::reqwest::header::{HeaderMap, HeaderValue};
17use matrix_sdk::ruma::{OwnedDeviceId, owned_server_name};
17use matrix_sdk::ruma::{DeviceId, OwnedDeviceId, owned_server_name};
1818use modalkit::crossterm;
1919use modalkit::env::vim::VimMode;
2020use modalkit::keybindings::InputKey;
@@ -278,6 +278,18 @@ pub struct Session {
278278 device_id: OwnedDeviceId,
279279}
280280
281/// A device ID saved at logout so that the next login can reuse it.
282///
283/// The SDK store is keyed by user and device ID, so logging back in with
284/// a freshly issued device ID would leave the existing store unusable
285/// (#775). Saving the ID lets the next login ask the homeserver for the
286/// same device and keep using that store.
287#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
288pub struct SavedDevice {
289 user_id: OwnedUserId,
290 device_id: OwnedDeviceId,
291}
292
281293impl From<Session> for MatrixSession {
282294 fn from(session: Session) -> Self {
283295 MatrixSession {
@@ -1203,6 +1215,7 @@ impl IambConfig {
12031215pub struct ApplicationSettings {
12041216 pub layout_json: PathBuf,
12051217 pub session_json: PathBuf,
1218 pub device_json: PathBuf,
12061219 pub session_json_old: PathBuf,
12071220 pub sled_dir: PathBuf,
12081221 pub sqlite_dir: PathBuf,
@@ -1343,6 +1356,9 @@ impl ApplicationSettings {
13431356 let mut session_json = profile_data_dir.clone();
13441357 session_json.push("session.json");
13451358
1359 let mut device_json = profile_data_dir;
1360 device_json.push("device.json");
1361
13461362 let mut session_json_old = profile_dir;
13471363 session_json_old.push("session.json");
13481364
@@ -1361,6 +1377,7 @@ impl ApplicationSettings {
13611377 sled_dir,
13621378 layout_json,
13631379 session_json,
1380 device_json,
13641381 session_json_old,
13651382 sqlite_dir,
13661383 sqlite_cache_dir,
@@ -1412,6 +1429,32 @@ impl ApplicationSettings {
14121429 Ok(())
14131430 }
14141431
1432 /// The device ID saved at logout, if it was saved for this profile's
1433 /// user. A missing or stale file is not an error: the next login then
1434 /// simply gets a fresh device ID from the homeserver.
1435 pub fn read_saved_device(&self) -> Option<OwnedDeviceId> {
1436 let file = File::open(self.device_json.as_path()).ok()?;
1437 let reader = BufReader::new(file);
1438 let saved: SavedDevice = serde_json::from_reader(reader).ok()?;
1439
1440 if saved.user_id == self.profile.user_id {
1441 Some(saved.device_id)
1442 } else {
1443 None
1444 }
1445 }
1446
1447 pub fn write_saved_device(&self, device_id: &DeviceId) -> Result<(), IambError> {
1448 let file = File::create(self.device_json.as_path())?;
1449 let writer = BufWriter::new(file);
1450 let saved = SavedDevice {
1451 user_id: self.profile.user_id.clone(),
1452 device_id: device_id.to_owned(),
1453 };
1454 serde_json::to_writer(writer, &saved).map_err(IambError::from)?;
1455 Ok(())
1456 }
1457
14151458 pub fn get_user_char_span(&self, user_id: &UserId) -> Span<'_> {
14161459 let (color, c) = self
14171460 .tunables
src/tests.rs +1
@@ -206,6 +206,7 @@ pub fn mock_settings() -> ApplicationSettings {
206206 ApplicationSettings {
207207 layout_json: PathBuf::new(),
208208 session_json: PathBuf::new(),
209 device_json: PathBuf::new(),
209210 session_json_old: PathBuf::new(),
210211 sled_dir: PathBuf::new(),
211212 sqlite_dir: PathBuf::new(),
src/worker.rs +20 −10
@@ -2118,18 +2118,19 @@ impl ClientWorker {
21182118 client.restore_session(session).await.map_err(IambError::from)?;
21192119 },
21202120 LoginStyle::Password(password) => {
2121 let resp = client
2121 let mut login = client
21222122 .matrix_auth()
21232123 .login_username(&self.settings.profile.user_id, &password)
2124 .initial_device_display_name(initial_devname().as_str())
2125 .send()
2126 .await
2127 .map_err(IambError::from)?;
2124 .initial_device_display_name(initial_devname().as_str());
2125 if let Some(device_id) = self.settings.read_saved_device() {
2126 login = login.device_id(device_id.as_str());
2127 }
2128 let resp = login.send().await.map_err(IambError::from)?;
21282129 let session = MatrixSession::from(&resp);
21292130 self.settings.write_session(session)?;
21302131 },
21312132 LoginStyle::SingleSignOn => {
2132 let resp = client
2133 let mut login = client
21332134 .matrix_auth()
21342135 .login_sso(|url| {
21352136 let opened = format!(
@@ -2142,10 +2143,11 @@ impl ClientWorker {
21422143 Ok(())
21432144 }
21442145 })
2145 .initial_device_display_name(initial_devname().as_str())
2146 .send()
2147 .await
2148 .map_err(IambError::from)?;
2146 .initial_device_display_name(initial_devname().as_str());
2147 if let Some(device_id) = self.settings.read_saved_device() {
2148 login = login.device_id(device_id.as_str());
2149 }
2150 let resp = login.send().await.map_err(IambError::from)?;
21492151
21502152 let session = MatrixSession::from(&resp);
21512153 self.settings.write_session(session)?;
@@ -2176,6 +2178,14 @@ impl ClientWorker {
21762178 return Err(err);
21772179 }
21782180
2181 // Capture the device ID before logging out, while the session is
2182 // still active. The next login reuses it, which keeps the existing
2183 // SDK store valid: the store is keyed by user and device ID, and a
2184 // freshly issued device ID would no longer match it.
2185 if let Some(device_id) = self.client.device_id() {
2186 self.settings.write_saved_device(device_id)?;
2187 }
2188
21792189 // Send the logout request.
21802190 if let Err(e) = self.client.matrix_auth().logout().await {
21812191 let msg = format!("Failed to logout: {e}");