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:
1 parent
fe7816f6c6
commit
3926209b0d
5 files changed
+212
-8
No files matched your search
@@ -0,0 +1,117 @@
|
||||
//! Legal settings storage
|
||||
//!
|
||||
//! Persists user acceptance of legal terms:
|
||||
//! - qobuz_tos_accepted: User has accepted Qobuz Terms of Service
|
||||
|
||||
use rusqlite::{Connection, params};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use log::info;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct LegalSettings {
|
||||
/// User has accepted Qobuz Terms of Service
|
||||
pub qobuz_tos_accepted: bool,
|
||||
}
|
||||
|
||||
impl Default for LegalSettings {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
qobuz_tos_accepted: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct LegalSettingsStore {
|
||||
conn: Connection,
|
||||
}
|
||||
|
||||
impl LegalSettingsStore {
|
||||
pub fn new() -> Result<Self, String> {
|
||||
let data_dir = dirs::data_dir()
|
||||
.ok_or("Could not determine data directory")?
|
||||
.join("qbz");
|
||||
|
||||
std::fs::create_dir_all(&data_dir)
|
||||
.map_err(|e| format!("Failed to create data directory: {}", e))?;
|
||||
|
||||
let db_path = data_dir.join("legal_settings.db");
|
||||
let conn = Connection::open(&db_path)
|
||||
.map_err(|e| format!("Failed to open legal settings database: {}", e))?;
|
||||
|
||||
// Create table
|
||||
conn.execute_batch(
|
||||
"CREATE TABLE IF NOT EXISTS legal_settings (
|
||||
id INTEGER PRIMARY KEY CHECK (id = 1),
|
||||
qobuz_tos_accepted INTEGER NOT NULL DEFAULT 0
|
||||
);"
|
||||
).map_err(|e| format!("Failed to create legal settings table: {}", e))?;
|
||||
|
||||
// Insert default row if it doesn't exist
|
||||
conn.execute(
|
||||
"INSERT OR IGNORE INTO legal_settings (id, qobuz_tos_accepted) VALUES (1, 0)",
|
||||
[]
|
||||
).map_err(|e| format!("Failed to insert default legal settings: {}", e))?;
|
||||
|
||||
info!("[LegalSettings] Database initialized");
|
||||
|
||||
Ok(Self { conn })
|
||||
}
|
||||
|
||||
pub fn get_settings(&self) -> Result<LegalSettings, String> {
|
||||
self.conn
|
||||
.query_row(
|
||||
"SELECT qobuz_tos_accepted FROM legal_settings WHERE id = 1",
|
||||
[],
|
||||
|row| {
|
||||
let qobuz_tos_accepted: i32 = row.get(0)?;
|
||||
Ok(LegalSettings {
|
||||
qobuz_tos_accepted: qobuz_tos_accepted != 0,
|
||||
})
|
||||
},
|
||||
)
|
||||
.map_err(|e| format!("Failed to get legal settings: {}", e))
|
||||
}
|
||||
|
||||
pub fn set_qobuz_tos_accepted(&self, accepted: bool) -> Result<(), String> {
|
||||
self.conn
|
||||
.execute(
|
||||
"UPDATE legal_settings SET qobuz_tos_accepted = ? WHERE id = 1",
|
||||
params![accepted as i32],
|
||||
)
|
||||
.map_err(|e| format!("Failed to update ToS acceptance: {}", e))?;
|
||||
|
||||
info!("[LegalSettings] Qobuz ToS accepted: {}", accepted);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub type LegalSettingsState = Arc<Mutex<LegalSettingsStore>>;
|
||||
|
||||
// Tauri commands
|
||||
|
||||
#[tauri::command]
|
||||
pub fn get_legal_settings(
|
||||
state: tauri::State<'_, LegalSettingsState>,
|
||||
) -> Result<LegalSettings, String> {
|
||||
let store = state.lock().map_err(|e| format!("Lock error: {}", e))?;
|
||||
store.get_settings()
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn get_qobuz_tos_accepted(
|
||||
state: tauri::State<'_, LegalSettingsState>,
|
||||
) -> Result<bool, String> {
|
||||
let store = state.lock().map_err(|e| format!("Lock error: {}", e))?;
|
||||
let settings = store.get_settings()?;
|
||||
Ok(settings.qobuz_tos_accepted)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn set_qobuz_tos_accepted(
|
||||
state: tauri::State<'_, LegalSettingsState>,
|
||||
accepted: bool,
|
||||
) -> Result<(), String> {
|
||||
let store = state.lock().map_err(|e| format!("Lock error: {}", e))?;
|
||||
store.set_qobuz_tos_accepted(accepted)
|
||||
}
|
||||
@@ -16,6 +16,7 @@ pub mod favorites_preferences;
|
||||
pub mod favorites_cache;
|
||||
pub mod subscription_state;
|
||||
pub mod tray_settings;
|
||||
pub mod legal_settings;
|
||||
|
||||
pub use audio_settings::{
|
||||
AudioSettings,
|
||||
@@ -83,3 +84,12 @@ pub use favorites_cache::{
|
||||
sync_cached_favorite_artists,
|
||||
clear_favorites_cache,
|
||||
};
|
||||
|
||||
pub use legal_settings::{
|
||||
LegalSettings,
|
||||
LegalSettingsState,
|
||||
LegalSettingsStore,
|
||||
get_legal_settings,
|
||||
get_qobuz_tos_accepted,
|
||||
set_qobuz_tos_accepted,
|
||||
};
|
||||
Reference in new issue
Block a user