add frontend per-user session lifecycle
- userStorage.ts: per-user localStorage scoping utility with migration - authStore: add userId to UserInfo interface - LoginView: extract user_id from login/auto_login responses - +page.svelte: call activate_user_session after login, deactivate_user_session before logout, set up per-user localStorage
This commit is contained in:
1 parent
b936490969
commit
8b3047b8fc
4 files changed
+135
-2
No files matched your search
@@ -8,6 +8,7 @@
|
||||
|
||||
interface UserInfo {
|
||||
userName: string;
|
||||
userId: number;
|
||||
subscription: string;
|
||||
subscriptionValidUntil?: string | null;
|
||||
}
|
||||
@@ -71,14 +72,16 @@
|
||||
if (loggedIn) {
|
||||
clearTimeoutTimer();
|
||||
const userInfo = await invoke<{ user_name: string; subscription: string; subscription_valid_until?: string | null } | null>('get_user_info');
|
||||
if (userInfo) {
|
||||
const userId = await invoke<number | null>('get_current_user_id');
|
||||
if (userInfo && userId) {
|
||||
onLoginSuccess({
|
||||
userName: userInfo.user_name,
|
||||
userId,
|
||||
subscription: userInfo.subscription,
|
||||
subscriptionValidUntil: userInfo.subscription_valid_until ?? null,
|
||||
});
|
||||
} else {
|
||||
onLoginSuccess({ userName: 'User', subscription: 'Active' });
|
||||
onLoginSuccess({ userName: 'User', userId: userId || 0, subscription: 'Active' });
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -96,6 +99,7 @@
|
||||
const response = await invoke<{
|
||||
success: boolean;
|
||||
user_name?: string;
|
||||
user_id?: number;
|
||||
subscription?: string;
|
||||
subscription_valid_until?: string | null;
|
||||
error?: string;
|
||||
@@ -107,6 +111,7 @@
|
||||
console.log('Auto-login successful');
|
||||
onLoginSuccess({
|
||||
userName: response.user_name || 'User',
|
||||
userId: response.user_id || 0,
|
||||
subscription: response.subscription || 'Active',
|
||||
subscriptionValidUntil: response.subscription_valid_until ?? null,
|
||||
});
|
||||
@@ -165,6 +170,7 @@
|
||||
const response = await invoke<{
|
||||
success: boolean;
|
||||
user_name?: string;
|
||||
user_id?: number;
|
||||
subscription?: string;
|
||||
subscription_valid_until?: string | null;
|
||||
error?: string;
|
||||
@@ -187,6 +193,7 @@
|
||||
|
||||
onLoginSuccess({
|
||||
userName: response.user_name || 'User',
|
||||
userId: response.user_id || 0,
|
||||
subscription: response.subscription || 'Active',
|
||||
subscriptionValidUntil: response.subscription_valid_until ?? null,
|
||||
});
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
export interface UserInfo {
|
||||
userName: string;
|
||||
userId: number;
|
||||
subscription: string;
|
||||
subscriptionValidUntil?: string | null;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
/**
|
||||
* Per-user localStorage scoping
|
||||
*
|
||||
* Prefixes localStorage keys with the active Qobuz user ID so that
|
||||
* different accounts get independent UI preferences.
|
||||
*
|
||||
* Keys follow the pattern: `qbz.{userId}.{originalKey}`
|
||||
* When no user is active, falls back to the original key (pre-migration).
|
||||
*/
|
||||
|
||||
let currentUserId: number | null = null;
|
||||
|
||||
const MIGRATION_MARKER_PREFIX = 'qbz.__migrated_to_user.';
|
||||
|
||||
/**
|
||||
* Set the active user ID for localStorage scoping.
|
||||
* Called after login, before any store reads.
|
||||
*/
|
||||
export function setStorageUserId(userId: number | null): void {
|
||||
currentUserId = userId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current storage user ID.
|
||||
*/
|
||||
export function getStorageUserId(): number | null {
|
||||
return currentUserId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a user-scoped key from an original key.
|
||||
* `qbz-theme` → `qbz.12345678.qbz-theme`
|
||||
*/
|
||||
function userKey(key: string): string {
|
||||
if (currentUserId === null) {
|
||||
return key;
|
||||
}
|
||||
return `qbz.${currentUserId}.${key}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a per-user localStorage value.
|
||||
*/
|
||||
export function getUserItem(key: string): string | null {
|
||||
return localStorage.getItem(userKey(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a per-user localStorage value.
|
||||
*/
|
||||
export function setUserItem(key: string, value: string): void {
|
||||
localStorage.setItem(userKey(key), value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a per-user localStorage value.
|
||||
*/
|
||||
export function removeUserItem(key: string): void {
|
||||
localStorage.removeItem(userKey(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* One-time migration of old global `qbz-*` keys to user-scoped keys.
|
||||
*
|
||||
* Copies (not moves) existing values so that a rollback to an older
|
||||
* version still works. The marker prevents re-running.
|
||||
*/
|
||||
export function migrateLocalStorage(userId: number): void {
|
||||
const marker = `${MIGRATION_MARKER_PREFIX}${userId}`;
|
||||
if (localStorage.getItem(marker)) {
|
||||
return;
|
||||
}
|
||||
|
||||
let migrated = 0;
|
||||
for (let i = 0; i < localStorage.length; i++) {
|
||||
const key = localStorage.key(i);
|
||||
if (!key || !key.startsWith('qbz-')) continue;
|
||||
// Skip keys that are already user-scoped
|
||||
if (key.startsWith('qbz.')) continue;
|
||||
|
||||
const value = localStorage.getItem(key);
|
||||
if (value !== null) {
|
||||
const scoped = `qbz.${userId}.${key}`;
|
||||
// Only copy if the user-scoped key doesn't already exist
|
||||
if (localStorage.getItem(scoped) === null) {
|
||||
localStorage.setItem(scoped, value);
|
||||
migrated++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
localStorage.setItem(marker, String(Date.now()));
|
||||
|
||||
if (migrated > 0) {
|
||||
console.log(`[userStorage] Migrated ${migrated} localStorage keys for user ${userId}`);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user