diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index c86486b..1ee0c80 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -453,43 +453,6 @@ {isDev ? "Dev Edition | " : ""}{currentDate} -
-
- - 📊 View Stats - - - {#if user} -
- -
- {:else} - - {/if} -
- - {#if isDev} -
-
Debug Info:
-
User: {user ? `${user.email} (ID: ${user.id})` : 'Not signed in'}
-
Session: {session ? `Expires ${session.expiresAt.toLocaleDateString()}` : 'No session'}
-
Anonymous ID: {anonymousId || 'Not set'}
-
- {/if} -
@@ -532,9 +495,44 @@ {/if}
- {#if isDev} - - {/if} +
+
+ + 📊 View Stats + + + {#if user} +
+ +
+ {:else} + + {/if} +
+ + {#if isDev} +
+
Debug Info:
+
User: {user ? `${user.email} (ID: ${user.id})` : 'Not signed in'}
+
Session: {session ? `Expires ${session.expiresAt.toLocaleDateString()}` : 'No session'}
+
Anonymous ID: {anonymousId || 'Not set'}
+
+ + {/if} +
diff --git a/src/routes/stats/+page.server.ts b/src/routes/stats/+page.server.ts index 2635ac8..976c78a 100644 --- a/src/routes/stats/+page.server.ts +++ b/src/routes/stats/+page.server.ts @@ -4,13 +4,20 @@ import { eq, desc } from 'drizzle-orm'; import type { PageServerLoad } from './$types'; export const load: PageServerLoad = async ({ url, locals }) => { - const userId = url.searchParams.get('userId'); - const anonymousId = url.searchParams.get('anonymousId'); + // Check if user is authenticated + if (!locals.user) { + return { + stats: null, + error: null, + user: null, + session: null, + requiresAuth: true + }; + } - // Prioritize userId (authenticated user) over anonymousId - const targetId = userId || anonymousId; + const userId = locals.user.id; - if (!targetId) { + if (!userId) { return { stats: null, error: 'No user ID provided', @@ -24,7 +31,7 @@ export const load: PageServerLoad = async ({ url, locals }) => { const completions = await db .select() .from(dailyCompletions) - .where(eq(dailyCompletions.anonymousId, targetId)) + .where(eq(dailyCompletions.anonymousId, userId)) .orderBy(desc(dailyCompletions.date)); if (completions.length === 0) { diff --git a/src/routes/stats/+page.svelte b/src/routes/stats/+page.svelte index 3bc8528..66c51aa 100644 --- a/src/routes/stats/+page.svelte +++ b/src/routes/stats/+page.svelte @@ -2,6 +2,8 @@ import { browser } from "$app/environment"; import { goto } from "$app/navigation"; import { onMount } from "svelte"; + import { enhance } from '$app/forms'; + import AuthModal from "$lib/components/AuthModal.svelte"; import { getGradeColor, formatDate, @@ -15,9 +17,11 @@ error?: string; user?: any; session?: any; + requiresAuth?: boolean; } let { data }: { data: PageData } = $props(); + let authModalOpen = $state(false); let loading = $state(true); @@ -33,28 +37,6 @@ } onMount(async () => { - const url = new URL(window.location.href); - const hasUserId = url.searchParams.get('userId'); - - // If user is authenticated, no need to check for anonymousId - if (data.user || hasUserId) { - loading = false; - return; - } - - // For anonymous users, ensure anonymousId is in URL - const anonymousId = getOrCreateAnonymousId(); - if (!anonymousId) { - goto("/"); - return; - } - - if (!url.searchParams.get('anonymousId')) { - url.searchParams.set('anonymousId', anonymousId); - goto(url.pathname + url.search); - return; - } - loading = false; }); @@ -91,6 +73,27 @@

Loading your stats...

+ {:else if data.requiresAuth} +
+
+

Authentication Required

+

You must be logged in to see your stats.

+
+ + + ← Back to Game + +
+
+
{:else if data.error}
@@ -213,4 +216,6 @@ {/if} {/if}
-
\ No newline at end of file + + + \ No newline at end of file