diff --git a/app/(sharing)/[token]/[id]/actions.ts b/app/(sharing)/[token]/[id]/actions.ts index 0d41245..8a80ee4 100644 --- a/app/(sharing)/[token]/[id]/actions.ts +++ b/app/(sharing)/[token]/[id]/actions.ts @@ -1,6 +1,4 @@ "use server"; - -import { getAuth } from "@/auth/queries/getauth"; import { artemis, daphnis } from "@/lib/prisma"; import type * as Prisma from "@prisma/client"; @@ -11,14 +9,40 @@ type LinkSharingToken = { playlogId: number; }; -export async function getSharedSong() { - const { user } = await getAuth(); - +export async function getSharedSong(token: string) { try { + const linkSharingToken = await daphnis.linkSharingToken.findFirst({ + where: { + token: token, + }, + select: { + playlogId: true, + userId: true, + }, + }); + + const user = await daphnis.user.findUnique({ + where: { + id: linkSharingToken!.userId, + }, + select: { + id: true, + UserId: true, + }, + }); + + if (!user) { + throw new Error("User not found"); + } + + if (!linkSharingToken) { + throw new Error("Invalid or expired token"); + } + const songs: ChuniScorePlaylog[] = await artemis.chuni_score_playlog.findMany({ where: { - user: user?.UserId, + user: user.UserId, }, orderBy: { userPlayDate: "desc", diff --git a/app/(sharing)/[token]/[id]/page.tsx b/app/(sharing)/[token]/[id]/page.tsx index 0e7d29b..45d454e 100644 --- a/app/(sharing)/[token]/[id]/page.tsx +++ b/app/(sharing)/[token]/[id]/page.tsx @@ -18,7 +18,6 @@ export default async function Share({ }) { const { token, id } = params; - // Verify the token const tokenResult = await shareScore(token); if (tokenResult.error) { @@ -27,10 +26,13 @@ export default async function Share({ const playlogId = parseInt(id); - const songsData = await getSharedSong(); + const songsData = await getSharedSong(token); const playlogIds = await getPlaylogId(playlogId); - // Filter songsData to match the playlogIds + if (!songsData) { + return
Error: Something went wrong
; + } + const chunithm: chunithm[] = songsData.filter((song) => playlogIds.includes(song.id), ); @@ -42,7 +44,7 @@ export default async function Share({ {chunithm.map((song) => (