diff --git a/src/routes/my-notes/+page.svelte b/src/routes/my-notes/+page.svelte index 852d31e..58b3634 100644 --- a/src/routes/my-notes/+page.svelte +++ b/src/routes/my-notes/+page.svelte @@ -13,6 +13,7 @@ let events: NDKEvent[] = $state([]); let loading = $state(true); let error: string | null = $state(null); + let checkingAuth = $state(true); // Track authentication check state - prevents premature redirects during auth restoration let showTags: Record = $state({}); let renderedContent: Record = $state({}); @@ -167,17 +168,52 @@ }); }); - // AI-NOTE: Check authentication status immediately and redirect if not logged in + // AI-NOTE: Check authentication status and redirect if not logged in + // Wait for authentication state to be properly initialized before checking + let authCheckTimeout: ReturnType | null = null; + $effect(() => { - const user = get(userStore); - if (!user.signedIn) { - // Redirect to home page if not logged in - goto('/'); + const user = $userStore; + + // Clear any existing timeout + if (authCheckTimeout) { + clearTimeout(authCheckTimeout); + authCheckTimeout = null; + } + + // If user is signed in, we're good + if (user.signedIn) { + checkingAuth = false; return; } + + // If user is not signed in, wait a bit for auth restoration to complete + // This handles the case where the page loads before auth restoration finishes + authCheckTimeout = setTimeout(() => { + const currentUser = get(userStore); + if (!currentUser.signedIn) { + console.debug('[MyNotes] User not signed in after auth restoration, redirecting to home page'); + goto('/'); + } else { + checkingAuth = false; + } + }, 1500); // 1.5 second delay to allow auth restoration to complete + + // Cleanup function + return () => { + if (authCheckTimeout) { + clearTimeout(authCheckTimeout); + authCheckTimeout = null; + } + }; }); - onMount(fetchMyNotes); + // AI-NOTE: Only fetch notes after authentication is confirmed + $effect(() => { + if (!checkingAuth && $userStore.signedIn) { + fetchMyNotes(); + } + });

My Notes

- {#if loading} + {#if checkingAuth} +
Checking authentication...
+ {:else if loading}
Loading…
{:else if error}
{error}