2024-06-29 05:22:22 +00:00
|
|
|
"use server";
|
|
|
|
|
|
|
|
import { getAuth } from "@/auth/queries/getauth";
|
2024-06-29 06:37:50 +00:00
|
|
|
import { daphnis } from "@/lib/prisma";
|
2024-06-29 05:22:22 +00:00
|
|
|
import { randomUUID } from "crypto";
|
|
|
|
import { randomBytes } from "crypto";
|
|
|
|
import { redirect } from "next/navigation";
|
|
|
|
|
|
|
|
export async function generateShareToken(id: number): Promise<{
|
|
|
|
token?: string;
|
|
|
|
id?: string;
|
|
|
|
error?: string;
|
|
|
|
}> {
|
|
|
|
const { user } = await getAuth();
|
|
|
|
|
|
|
|
if (!user || !user.id || typeof user.id !== "string") {
|
|
|
|
return {
|
|
|
|
error: "Invalid user or user ID",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const gernatetoken = randomBytes(5).readUInt32BE(0).toString();
|
2024-06-29 06:37:50 +00:00
|
|
|
const token = await daphnis.linkSharingToken.create({
|
2024-06-29 05:22:22 +00:00
|
|
|
data: {
|
|
|
|
playlogId: id,
|
|
|
|
id: randomUUID(),
|
|
|
|
userId: user.id,
|
|
|
|
token: gernatetoken,
|
|
|
|
createdAt: new Date(),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return { token: token.token };
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function shareScore(token: string) {
|
2024-06-29 06:37:50 +00:00
|
|
|
const PublicPage = await daphnis.linkSharingToken.findUnique({
|
2024-06-29 05:22:22 +00:00
|
|
|
where: {
|
|
|
|
token,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!PublicPage) {
|
|
|
|
return {
|
|
|
|
error: "Invalid token or token does not exist",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if token has expired
|
|
|
|
const tokenAge =
|
|
|
|
new Date().getTime() - new Date(PublicPage.createdAt).getTime();
|
|
|
|
const tokenAgeLimit = 1000 * 60 * 60 * 24; // 1 day in milliseconds
|
|
|
|
|
|
|
|
if (tokenAge > tokenAgeLimit) {
|
2024-06-29 06:37:50 +00:00
|
|
|
await daphnis.linkSharingToken.update({
|
2024-06-29 05:22:22 +00:00
|
|
|
where: {
|
|
|
|
token,
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
tokenExpiredAt: new Date(),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
redirect("/");
|
|
|
|
}
|
|
|
|
|
|
|
|
return { success: true };
|
|
|
|
}
|