From e98ea146e34f0a2e3ce5b82f51054c1563fdd4f6 Mon Sep 17 00:00:00 2001 From: vicrodh <103399283+vicrodh@users.noreply.github.com> Date: Sat, 10 Jan 2026 04:30:11 -0600 Subject: [PATCH] refactor: extract authentication state into authStore - Create authStore.ts for managing login state and user info - Implement setLoggedIn and setLoggedOut actions - Update +page.svelte to use store subscription - Simplify handleLoginSuccess and handleLogout handlers --- src/lib/stores/authStore.ts | 76 +++++++++++++++++++++++++++++++++++++ src/routes/+page.svelte | 29 ++++++++++---- 2 files changed, 98 insertions(+), 7 deletions(-) create mode 100644 src/lib/stores/authStore.ts diff --git a/src/lib/stores/authStore.ts b/src/lib/stores/authStore.ts new file mode 100644 index 000000000..7de23f0f8 --- /dev/null +++ b/src/lib/stores/authStore.ts @@ -0,0 +1,76 @@ +/** + * Authentication State Store + * + * Manages user authentication state and user info. + */ + +export interface UserInfo { + userName: string; + subscription: string; +} + +// Auth state +let isLoggedIn = false; +let userInfo: UserInfo | null = null; + +// Listeners +const listeners = new Set<() => void>(); + +function notifyListeners(): void { + for (const listener of listeners) { + listener(); + } +} + +/** + * Subscribe to auth state changes + */ +export function subscribe(listener: () => void): () => void { + listeners.add(listener); + listener(); // Immediately notify with current state + return () => listeners.delete(listener); +} + +// ============ Getters ============ + +export function getIsLoggedIn(): boolean { + return isLoggedIn; +} + +export function getUserInfo(): UserInfo | null { + return userInfo; +} + +// ============ Actions ============ + +/** + * Set login state after successful authentication + */ +export function setLoggedIn(info: UserInfo): void { + isLoggedIn = true; + userInfo = info; + notifyListeners(); +} + +/** + * Clear auth state on logout + */ +export function setLoggedOut(): void { + isLoggedIn = false; + userInfo = null; + notifyListeners(); +} + +// ============ State Getter ============ + +export interface AuthState { + isLoggedIn: boolean; + userInfo: UserInfo | null; +} + +export function getAuthState(): AuthState { + return { + isLoggedIn, + userInfo + }; +} diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index 7c42d5ad5..5a0955a24 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -44,6 +44,15 @@ type UIState } from '$lib/stores/uiStore'; + // Auth state management + import { + subscribe as subscribeAuth, + setLoggedIn, + setLoggedOut, + getAuthState, + type UserInfo + } from '$lib/stores/authStore'; + // Components import Sidebar from '$lib/components/Sidebar.svelte'; import NowPlayingBar from '$lib/components/NowPlayingBar.svelte'; @@ -219,9 +228,9 @@ samplingRate?: number; } - // Auth State + // Auth State (from authStore subscription) let isLoggedIn = $state(false); - let userInfo = $state<{ userName: string; subscription: string } | null>(null); + let userInfo = $state(null); // View State type ViewType = 'home' | 'search' | 'library' | 'settings' | 'album' | 'artist' | 'playlist' | 'favorites'; @@ -1340,9 +1349,8 @@ } // Auth Handlers - function handleLoginSuccess(info: { userName: string; subscription: string }) { - isLoggedIn = true; - userInfo = info; + function handleLoginSuccess(info: UserInfo) { + setLoggedIn(info); showToast(`Welcome, ${info.userName}!`, 'success'); } @@ -1357,8 +1365,7 @@ console.error('Failed to clear credentials:', clearErr); // Don't block logout if clearing fails } - isLoggedIn = false; - userInfo = null; + setLoggedOut(); currentTrack = null; isPlaying = false; showToast('Logged out successfully', 'info'); @@ -1530,6 +1537,13 @@ playlistModalTrackIds = uiState.playlistModalTrackIds; }); + // Subscribe to auth state changes + const unsubscribeAuth = subscribeAuth(() => { + const authState = getAuthState(); + isLoggedIn = authState.isLoggedIn; + userInfo = authState.userInfo; + }); + // Restore Last.fm session on app startup (async () => { try { @@ -1562,6 +1576,7 @@ unsubscribeDownloads(); unsubscribeToast(); unsubscribeUI(); + unsubscribeAuth(); }; });