Files
daphnis/app/(sharing)/[token]/token.ts
2024-09-05 21:43:53 -04:00

52 lines
1.2 KiB
TypeScript

"use server";
import { getAuth } from "@/auth/queries/getauth";
import { daphnis } from "@/lib/prisma";
import { randomUUID } from "crypto";
import { randomBytes } from "crypto";
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",
};
}
// generate the token that expires
const gernatetoken = randomBytes(5).readUInt32BE(0).toString();
// generate token logic
const token = await daphnis.linkSharingToken.create({
data: {
playlogId: id, // sets the playlog id
id: randomUUID(), // generates a random primary id for the share token
userId: user.id, // attaches the userid from daphnis
token: gernatetoken, // makes an expirable token thats added to the token column
createdAt: new Date(), // created at date
},
});
return { token: token.token };
}
export async function shareScore(token: string) {
const PageIsValid = await daphnis.linkSharingToken.findUnique({
where: {
token,
},
});
if (!PageIsValid) {
return {
error: "404",
};
}
return { success: true };
}