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
This commit is contained in:
1 parent
6edfb5db06
commit
e98ea146e3
2 files changed
+98
-7
No files matched your search
@@ -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
|
||||||
|
};
|
||||||
|
}
|
||||||
+22
-7
@@ -44,6 +44,15 @@
|
|||||||
type UIState
|
type UIState
|
||||||
} from '$lib/stores/uiStore';
|
} from '$lib/stores/uiStore';
|
||||||
|
|
||||||
|
// Auth state management
|
||||||
|
import {
|
||||||
|
subscribe as subscribeAuth,
|
||||||
|
setLoggedIn,
|
||||||
|
setLoggedOut,
|
||||||
|
getAuthState,
|
||||||
|
type UserInfo
|
||||||
|
} from '$lib/stores/authStore';
|
||||||
|
|
||||||
// Components
|
// Components
|
||||||
import Sidebar from '$lib/components/Sidebar.svelte';
|
import Sidebar from '$lib/components/Sidebar.svelte';
|
||||||
import NowPlayingBar from '$lib/components/NowPlayingBar.svelte';
|
import NowPlayingBar from '$lib/components/NowPlayingBar.svelte';
|
||||||
@@ -219,9 +228,9 @@
|
|||||||
samplingRate?: number;
|
samplingRate?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Auth State
|
// Auth State (from authStore subscription)
|
||||||
let isLoggedIn = $state(false);
|
let isLoggedIn = $state(false);
|
||||||
let userInfo = $state<{ userName: string; subscription: string } | null>(null);
|
let userInfo = $state<UserInfo | null>(null);
|
||||||
|
|
||||||
// View State
|
// View State
|
||||||
type ViewType = 'home' | 'search' | 'library' | 'settings' | 'album' | 'artist' | 'playlist' | 'favorites';
|
type ViewType = 'home' | 'search' | 'library' | 'settings' | 'album' | 'artist' | 'playlist' | 'favorites';
|
||||||
@@ -1340,9 +1349,8 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Auth Handlers
|
// Auth Handlers
|
||||||
function handleLoginSuccess(info: { userName: string; subscription: string }) {
|
function handleLoginSuccess(info: UserInfo) {
|
||||||
isLoggedIn = true;
|
setLoggedIn(info);
|
||||||
userInfo = info;
|
|
||||||
showToast(`Welcome, ${info.userName}!`, 'success');
|
showToast(`Welcome, ${info.userName}!`, 'success');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1357,8 +1365,7 @@
|
|||||||
console.error('Failed to clear credentials:', clearErr);
|
console.error('Failed to clear credentials:', clearErr);
|
||||||
// Don't block logout if clearing fails
|
// Don't block logout if clearing fails
|
||||||
}
|
}
|
||||||
isLoggedIn = false;
|
setLoggedOut();
|
||||||
userInfo = null;
|
|
||||||
currentTrack = null;
|
currentTrack = null;
|
||||||
isPlaying = false;
|
isPlaying = false;
|
||||||
showToast('Logged out successfully', 'info');
|
showToast('Logged out successfully', 'info');
|
||||||
@@ -1530,6 +1537,13 @@
|
|||||||
playlistModalTrackIds = uiState.playlistModalTrackIds;
|
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
|
// Restore Last.fm session on app startup
|
||||||
(async () => {
|
(async () => {
|
||||||
try {
|
try {
|
||||||
@@ -1562,6 +1576,7 @@
|
|||||||
unsubscribeDownloads();
|
unsubscribeDownloads();
|
||||||
unsubscribeToast();
|
unsubscribeToast();
|
||||||
unsubscribeUI();
|
unsubscribeUI();
|
||||||
|
unsubscribeAuth();
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user