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:
vicrodh committed 2026-01-10 04:30:11 -06:00
1 parent 6edfb5db06
commit e98ea146e3
2 files changed
+98 -7

No files matched your search

+76
View File
@@ -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
};
}