Support authenticated users in stats and page loading

This commit is contained in:
George Powell
2026-02-05 17:46:53 -05:00
parent 86f81cf9dd
commit 96024d5048
3 changed files with 35 additions and 11 deletions

View File

@@ -34,14 +34,16 @@ async function getTodayVerse(): Promise<DailyVerse> {
return inserted; return inserted;
} }
export const load: PageServerLoad = async () => { export const load: PageServerLoad = async ({ locals }) => {
const dailyVerse = await getTodayVerse(); const dailyVerse = await getTodayVerse();
const correctBook = getBookById(dailyVerse.bookId) ?? null; const correctBook = getBookById(dailyVerse.bookId) ?? null;
return { return {
dailyVerse, dailyVerse,
correctBookId: dailyVerse.bookId, correctBookId: dailyVerse.bookId,
correctBook correctBook,
user: locals.user,
session: locals.session
}; };
}; };

View File

@@ -3,13 +3,19 @@ import { dailyCompletions, type DailyCompletion } from '$lib/server/db/schema';
import { eq, desc } from 'drizzle-orm'; import { eq, desc } from 'drizzle-orm';
import type { PageServerLoad } from './$types'; 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'); const anonymousId = url.searchParams.get('anonymousId');
if (!anonymousId) { // Prioritize userId (authenticated user) over anonymousId
const targetId = userId || anonymousId;
if (!targetId) {
return { return {
stats: null, 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 const completions = await db
.select() .select()
.from(dailyCompletions) .from(dailyCompletions)
.where(eq(dailyCompletions.anonymousId, anonymousId)) .where(eq(dailyCompletions.anonymousId, targetId))
.orderBy(desc(dailyCompletions.date)); .orderBy(desc(dailyCompletions.date));
if (completions.length === 0) { if (completions.length === 0) {
@@ -39,7 +45,9 @@ export const load: PageServerLoad = async ({ url }) => {
currentStreak: 0, currentStreak: 0,
bestStreak: 0, bestStreak: 0,
recentCompletions: [] recentCompletions: []
} },
user: locals.user,
session: locals.session
}; };
} }
@@ -126,14 +134,18 @@ export const load: PageServerLoad = async ({ url }) => {
currentStreak, currentStreak,
bestStreak, bestStreak,
recentCompletions recentCompletions
} },
user: locals.user,
session: locals.session
}; };
} catch (error) { } catch (error) {
console.error('Error fetching user stats:', error); console.error('Error fetching user stats:', error);
return { return {
stats: null, stats: null,
error: 'Failed to fetch stats' error: 'Failed to fetch stats',
user: locals.user,
session: locals.session
}; };
} }
}; };

View File

@@ -13,6 +13,8 @@
interface PageData { interface PageData {
stats: UserStats | null; stats: UserStats | null;
error?: string; error?: string;
user?: any;
session?: any;
} }
let { data }: { data: PageData } = $props(); let { data }: { data: PageData } = $props();
@@ -31,14 +33,22 @@
} }
onMount(async () => { 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(); const anonymousId = getOrCreateAnonymousId();
if (!anonymousId) { if (!anonymousId) {
goto("/"); goto("/");
return; return;
} }
// If no anonymousId in URL, redirect with it
const url = new URL(window.location.href);
if (!url.searchParams.get('anonymousId')) { if (!url.searchParams.get('anonymousId')) {
url.searchParams.set('anonymousId', anonymousId); url.searchParams.set('anonymousId', anonymousId);
goto(url.pathname + url.search); goto(url.pathname + url.search);