diff --git a/src/lib/components/app/auth-dialog.svelte b/src/lib/components/app/auth-dialog.svelte index 102c4ae..4897b81 100644 --- a/src/lib/components/app/auth-dialog.svelte +++ b/src/lib/components/app/auth-dialog.svelte @@ -156,7 +156,7 @@ 'passkey-sign-in', () => authClient.signIn.passkey({ - autoFill: true, + autoFill: false, }), 'Passkey sign-in failed' ); diff --git a/src/lib/server/auth.ts b/src/lib/server/auth.ts index 944a121..66575c4 100644 --- a/src/lib/server/auth.ts +++ b/src/lib/server/auth.ts @@ -174,7 +174,13 @@ const accountNumber = () => }, }) satisfies BetterAuthPlugin; -const createAuthConfig = (baseURL = env.ORIGIN) => +const getAuthBaseURL = (requestOrigin?: string) => { + if (dev) return requestOrigin ?? env.ORIGIN ?? 'http://localhost:5173'; + + return env.ORIGIN || requestOrigin || 'http://localhost'; +}; + +const createAuthConfig = (baseURL: string) => ({ baseURL, secret: env.BETTER_AUTH_SECRET, @@ -218,9 +224,9 @@ const createAuthConfig = (baseURL = env.ORIGIN) => ], }) satisfies Omit[0], 'database'>; -export const createAuth = (d1: D1Database, baseURL = env.ORIGIN) => +export const createAuth = (d1: D1Database, requestOrigin?: string) => betterAuth({ - ...createAuthConfig(baseURL), + ...createAuthConfig(getAuthBaseURL(requestOrigin)), database: drizzleAdapter(getDb(d1), { provider: 'sqlite' }), }); diff --git a/src/lib/stations/chillhop.ts b/src/lib/stations/chillhop.ts index 4ea9e5c..02f54eb 100644 --- a/src/lib/stations/chillhop.ts +++ b/src/lib/stations/chillhop.ts @@ -8,6 +8,7 @@ import { and, eq } from 'drizzle-orm'; export async function getChillhopStation(id: number): Promise { const res = await fetch(`https://stream.chillhop.com/live/${id}`); const data = (await res.json()) as CHSong[]; + const event = getRequestEvent(); const finalData = data.map((song) => ({ fileId: String(song.fileId), @@ -20,19 +21,26 @@ export async function getChillhopStation(id: number): Promise { duration: song.duration ?? 0, })) as Song[]; - // should not await because it doesn't need to be done before returning the data - for (const song of finalData) { - analyticsData(song).catch((err) => { - console.error('Failed to store analytics data for song:', song.title, err); - }); - } + const db = getRequestDb(); + const analyticsPromise = Promise.all( + finalData.map((song) => + analyticsData(db, song).catch((err) => { + console.error('Failed to store analytics data for song:', song.title, err); + }), + ), + ).then(() => undefined); + + event.platform?.ctx.waitUntil(analyticsPromise); return finalData; } -async function analyticsData(song: Song) { - const db = getRequestDb(); - const existingSong = await db.select().from(songIds).where(and(eq(songIds.title, song.title), eq(songIds.artists, song.artists))).get(); +async function analyticsData(db: ReturnType, song: Song) { + const existingSong = await db + .select() + .from(songIds) + .where(and(eq(songIds.title, song.title), eq(songIds.artists, song.artists))) + .get(); if (existingSong) return; diff --git a/wrangler.jsonc b/wrangler.jsonc index 557a71b..0e4d0a2 100644 --- a/wrangler.jsonc +++ b/wrangler.jsonc @@ -13,6 +13,9 @@ "observability": { "enabled": true, }, + "vars": { + "ORIGIN": "https://lofi.srizan.dev", + }, "routes": [ { "pattern": "lofi.srizan.dev",