persist last user_id for session restore across app restarts

Saves the active user_id to a flat-path file on activate_user_session
and clears it on deactivate_user_session (explicit logout). On startup,
if saved credentials and a last_user_id both exist, the per-user session
is restored before loading ToS acceptance or attempting auto-login.

This fixes the chicken-and-egg problem where ToS acceptance lived in the
per-user DB but was needed before login to allow auto-login.
This commit is contained in:
vicrodh committed 2026-02-06 00:37:52 -06:00
1 parent 03736ef8f2
commit b6d2e10471
4 files changed
+62 -6

No files matched your search

+14 -1
View File
@@ -55,6 +55,13 @@ fn teardown_type_alias_state<S>(state: &Arc<Mutex<Option<S>>>) {
}
}
/// Get the last active user_id for session restore on startup.
/// Returns None if no previous session or after explicit logout.
#[tauri::command]
pub fn get_last_user_id() -> Option<u64> {
UserDataPaths::load_last_user_id()
}
/// Activate the per-user session after login.
///
/// This runs the one-time migration (if needed) and initializes all
@@ -168,6 +175,11 @@ pub async fn activate_user_session(
}
}
// Persist last user_id for session restore on next launch
if let Err(e) = UserDataPaths::save_last_user_id(user_id) {
log::warn!("Failed to save last_user_id: {}", e);
}
log::info!("User session activated for user_id={}", user_id);
Ok(())
}
@@ -229,8 +241,9 @@ pub async fn deactivate_user_session(
teardown_type_alias_state(&*download_settings);
teardown_type_alias_state(&*legal_settings);
// Clear the active user
// Clear the active user and persisted last_user_id
user_paths.clear_user();
UserDataPaths::clear_last_user_id();
log::info!("User session deactivated");
Ok(())
+1
View File
@@ -429,6 +429,7 @@ pub fn run() {
commands::clear_saved_credentials,
commands::auto_login,
// User session lifecycle
commands::get_last_user_id,
commands::activate_user_session,
commands::deactivate_user_session,
// Search commands
+30
View File
@@ -82,4 +82,34 @@ impl UserDataPaths {
.ok_or_else(|| "Could not determine cache directory".to_string())
.map(|d| d.join("qbz"))
}
/// Save the last active user_id to a flat-path file so the session
/// can be restored on next app launch (when "remember me" is active).
pub fn save_last_user_id(user_id: u64) -> Result<(), String> {
let path = Self::last_user_id_path()?;
std::fs::write(&path, user_id.to_string())
.map_err(|e| format!("Failed to save last user id: {}", e))?;
log::info!("Saved last_user_id={} to {:?}", user_id, path);
Ok(())
}
/// Read the last active user_id (returns None if file doesn't exist or is invalid).
pub fn load_last_user_id() -> Option<u64> {
let path = Self::last_user_id_path().ok()?;
let contents = std::fs::read_to_string(&path).ok()?;
contents.trim().parse::<u64>().ok()
}
/// Clear the last user_id file (called on explicit logout).
pub fn clear_last_user_id() {
if let Ok(path) = Self::last_user_id_path() {
let _ = std::fs::remove_file(&path);
log::info!("Cleared last_user_id file");
}
}
fn last_user_id_path() -> Result<PathBuf, String> {
let dir = Self::global_data_dir()?;
Ok(dir.join("last_user_id"))
}
}
+17 -5
View File
@@ -86,13 +86,25 @@
return;
}
// Load ToS acceptance from Rust (persisted, survives app updates)
initStatus = 'Loading preferences...';
await loadTosAcceptance();
// Check for saved credentials and auto-login
// Check for saved credentials and last user session
initStatus = 'Checking saved credentials...';
const hasSavedCreds = await invoke<boolean>('has_saved_credentials');
const lastUserId = await invoke<number | null>('get_last_user_id');
// Restore per-user session before reading ToS or auto-login
if (hasSavedCreds && lastUserId) {
initStatus = 'Restoring session...';
try {
await invoke('activate_user_session', { userId: lastUserId });
console.log('Restored user session for', lastUserId);
} catch (e) {
console.warn('Failed to restore user session:', e);
}
}
// Load ToS acceptance from Rust (now available after session restore)
initStatus = 'Loading preferences...';
await loadTosAcceptance();
if (hasSavedCreds && get(qobuzTosAccepted)) {
initStatus = 'Logging in...';