fix(auth): persist ToS acceptance in Rust/SQLite backend

ToS acceptance was stored in localStorage which gets cleared on app
updates, causing users to re-login frequently. Now stores ToS state
in SQLite (~/.local/share/qbz/legal_settings.db) which survives
updates.

- Add legal_settings.rs with SQLite storage for ToS acceptance
- Update qobuzLegalStore.ts to use Rust invoke commands
- LoginView now loads ToS from Rust before auto-login check
- Migration from localStorage for existing users

Closes #31
This commit is contained in:
vicrodh committed 2026-01-30 14:49:39 -06:00
1 parent fe7816f6c6
commit 3926209b0d
5 files changed
+212 -8

No files matched your search

+68 -7
View File
@@ -1,18 +1,79 @@
import { browser } from '$app/environment';
import { writable } from 'svelte/store';
import { writable, get } from 'svelte/store';
import { invoke } from '@tauri-apps/api/core';
const STORAGE_KEY = 'qbz-qobuz-tos-accepted';
// Store for ToS acceptance - persisted in Rust (survives app updates)
export const qobuzTosAccepted = writable<boolean>(false);
function readInitial(): boolean {
// Flag to track if we've loaded from Rust
let initialized = false;
/**
* Load ToS acceptance state from Rust backend.
* Should be called early in app initialization.
*/
export async function loadTosAcceptance(): Promise<boolean> {
if (!browser) return false;
return localStorage.getItem(STORAGE_KEY) === 'true';
try {
const accepted = await invoke<boolean>('get_qobuz_tos_accepted');
qobuzTosAccepted.set(accepted);
initialized = true;
return accepted;
} catch (err) {
console.error('Failed to load ToS acceptance from backend:', err);
// Fallback to localStorage for migration from old versions
const localValue = localStorage.getItem('qbz-qobuz-tos-accepted') === 'true';
if (localValue) {
// Migrate to Rust storage
try {
await invoke('set_qobuz_tos_accepted', { accepted: true });
qobuzTosAccepted.set(true);
localStorage.removeItem('qbz-qobuz-tos-accepted');
console.log('Migrated ToS acceptance to Rust storage');
return true;
} catch (migrateErr) {
console.error('Failed to migrate ToS acceptance:', migrateErr);
}
}
initialized = true;
return localValue;
}
}
export const qobuzTosAccepted = writable<boolean>(readInitial());
/**
* Set ToS acceptance state in Rust backend.
*/
export async function setTosAcceptance(accepted: boolean): Promise<void> {
if (!browser) return;
try {
await invoke('set_qobuz_tos_accepted', { accepted });
qobuzTosAccepted.set(accepted);
} catch (err) {
console.error('Failed to save ToS acceptance:', err);
throw err;
}
}
/**
* Subscribe to changes and persist to Rust.
* Only persists if value actually changed and we're initialized.
*/
if (browser) {
let lastValue: boolean | null = null;
qobuzTosAccepted.subscribe((value) => {
localStorage.setItem(STORAGE_KEY, String(value));
// Skip if not initialized or value hasn't changed
if (!initialized || lastValue === value) {
lastValue = value;
return;
}
lastValue = value;
// Persist to Rust (fire and forget, errors logged in setTosAcceptance)
invoke('set_qobuz_tos_accepted', { accepted: value }).catch((err) => {
console.error('Failed to persist ToS acceptance:', err);
});
});
}