From 90ae8fc48ccb2a565b69938f2f1c9fa6e924245b Mon Sep 17 00:00:00 2001 From: Polaris Date: Mon, 26 Aug 2024 13:08:43 -0400 Subject: [PATCH] fixed link sharing page not showing other users scores when logged in --- app/(sharing)/[token]/[id]/actions.ts | 36 +++++++++++++++++++++----- app/(sharing)/[token]/[id]/page.tsx | 12 +++++---- components/scoreplaylog/moreAction.tsx | 13 +++++----- 3 files changed, 44 insertions(+), 17 deletions(-) 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) => (
- Song: {song.title} + {song.title}
))} @@ -52,7 +54,7 @@ export default async function Share({ {chunithm.map((song) => (
- Artist: {song.artist} + {song.artist}
))} diff --git a/components/scoreplaylog/moreAction.tsx b/components/scoreplaylog/moreAction.tsx index a1625d6..0c35983 100644 --- a/components/scoreplaylog/moreAction.tsx +++ b/components/scoreplaylog/moreAction.tsx @@ -21,15 +21,16 @@ const ActionsCell: React.FC = ({ row }) => { const router = useRouter(); const handleGenerateShareToken = async () => { - const { token, error } = await generateShareToken(row.original.id); - if (error) { - setError(error); - } else { - const newTab = window.open(`/${token}/${row.original.id}`); + const response = await generateShareToken(row.original.id); + if (response.error) { + setError(response.error); + } else if (response.token) { + const shareUrl = `/${response.token}/${row.original.id}`; + const newTab = window.open(shareUrl, "_blank"); if (newTab) { newTab.focus(); } else { - router.push(`/${token}/${row.original.id}`); + router.push(shareUrl); } } };