From 96024d5048397a7c67cb734294f27422fd27d1dc Mon Sep 17 00:00:00 2001 From: George Powell Date: Thu, 5 Feb 2026 17:46:53 -0500 Subject: [PATCH] Support authenticated users in stats and page loading --- src/routes/+page.server.ts | 6 ++++-- src/routes/stats/+page.server.ts | 26 +++++++++++++++++++------- src/routes/stats/+page.svelte | 14 ++++++++++++-- 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/routes/+page.server.ts b/src/routes/+page.server.ts index 33387c2..2254638 100644 --- a/src/routes/+page.server.ts +++ b/src/routes/+page.server.ts @@ -34,14 +34,16 @@ async function getTodayVerse(): Promise { return inserted; } -export const load: PageServerLoad = async () => { +export const load: PageServerLoad = async ({ locals }) => { const dailyVerse = await getTodayVerse(); const correctBook = getBookById(dailyVerse.bookId) ?? null; return { dailyVerse, correctBookId: dailyVerse.bookId, - correctBook + correctBook, + user: locals.user, + session: locals.session }; }; diff --git a/src/routes/stats/+page.server.ts b/src/routes/stats/+page.server.ts index d263139..2635ac8 100644 --- a/src/routes/stats/+page.server.ts +++ b/src/routes/stats/+page.server.ts @@ -3,13 +3,19 @@ import { dailyCompletions, type DailyCompletion } from '$lib/server/db/schema'; import { eq, desc } from 'drizzle-orm'; import type { PageServerLoad } from './$types'; -export const load: PageServerLoad = async ({ url }) => { +export const load: PageServerLoad = async ({ url, locals }) => { + const userId = url.searchParams.get('userId'); const anonymousId = url.searchParams.get('anonymousId'); - if (!anonymousId) { + // Prioritize userId (authenticated user) over anonymousId + const targetId = userId || anonymousId; + + if (!targetId) { return { stats: null, - error: 'No anonymous ID provided' + error: 'No user ID provided', + user: locals.user, + session: locals.session }; } @@ -18,7 +24,7 @@ export const load: PageServerLoad = async ({ url }) => { const completions = await db .select() .from(dailyCompletions) - .where(eq(dailyCompletions.anonymousId, anonymousId)) + .where(eq(dailyCompletions.anonymousId, targetId)) .orderBy(desc(dailyCompletions.date)); if (completions.length === 0) { @@ -39,7 +45,9 @@ export const load: PageServerLoad = async ({ url }) => { currentStreak: 0, bestStreak: 0, recentCompletions: [] - } + }, + user: locals.user, + session: locals.session }; } @@ -126,14 +134,18 @@ export const load: PageServerLoad = async ({ url }) => { currentStreak, bestStreak, recentCompletions - } + }, + user: locals.user, + session: locals.session }; } catch (error) { console.error('Error fetching user stats:', error); return { stats: null, - error: 'Failed to fetch stats' + error: 'Failed to fetch stats', + user: locals.user, + session: locals.session }; } }; diff --git a/src/routes/stats/+page.svelte b/src/routes/stats/+page.svelte index a0753d8..3bc8528 100644 --- a/src/routes/stats/+page.svelte +++ b/src/routes/stats/+page.svelte @@ -13,6 +13,8 @@ interface PageData { stats: UserStats | null; error?: string; + user?: any; + session?: any; } let { data }: { data: PageData } = $props(); @@ -31,14 +33,22 @@ } 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 no anonymousId in URL, redirect with it - const url = new URL(window.location.href); if (!url.searchParams.get('anonymousId')) { url.searchParams.set('anonymousId', anonymousId); goto(url.pathname + url.search);