fix: passkey login not working + songs not saving to db

This commit is contained in:
2026-04-24 23:45:16 +02:00
parent a57c4bc5ed
commit 7d9048d67b
4 changed files with 30 additions and 13 deletions

View File

@@ -156,7 +156,7 @@
'passkey-sign-in',
() =>
authClient.signIn.passkey({
autoFill: true,
autoFill: false,
}),
'Passkey sign-in failed'
);

View File

@@ -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<Parameters<typeof betterAuth>[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' }),
});

View File

@@ -8,6 +8,7 @@ import { and, eq } from 'drizzle-orm';
export async function getChillhopStation(id: number): Promise<Song[]> {
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<Song[]> {
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<typeof getRequestDb>, 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;

View File

@@ -13,6 +13,9 @@
"observability": {
"enabled": true,
},
"vars": {
"ORIGIN": "https://lofi.srizan.dev",
},
"routes": [
{
"pattern": "lofi.srizan.dev",