Files
otunes/apps/web/lib/server-song.ts
T

24 lines
828 B
TypeScript

import { convexAuthNextjsToken } from "@convex-dev/auth/nextjs/server"
import { api } from "@otunes/convex/_generated/api"
import type { Id } from "@otunes/convex/_generated/dataModel"
import { fetchQuery } from "convex/nextjs"
import { convexServerOptions } from "@/lib/convex-server"
import type { SongSource } from "@/lib/server-hls"
/**
* Loads a song's file paths as the requesting listener, so Convex enforces auth
* even if the proxy is bypassed. Throws when signed out or the song is missing.
*/
export async function getSongSource(id: string): Promise<SongSource> {
const token = await convexAuthNextjsToken()
const song = await fetchQuery(
api.library.getSongSource,
{ id: id as Id<"songs"> },
convexServerOptions(token)
)
if (!song) {
throw new Error("Song not found")
}
return song
}