chatbot-template/app/actions.ts

28 lines
574 B
TypeScript
Raw Normal View History

2023-05-19 12:33:56 -04:00
"use server";
2023-06-02 15:15:35 -04:00
import { kv } from "@vercel/kv";
2023-05-19 12:33:56 -04:00
import { revalidatePath } from "next/cache";
2023-06-02 14:28:05 -04:00
export async function removeChat({
id,
path,
userId,
}: {
id: string;
userId: string;
path: string;
}) {
2023-06-02 15:15:35 -04:00
// @todo next-auth@v5 doesn't work in server actions yet
2023-06-02 14:28:05 -04:00
// const session = await auth();
2023-06-02 15:15:35 -04:00
const uid = await kv.hget<string>(`chat:${id}`, "userId");
if (uid !== userId) {
throw new Error("Unauthorized");
}
await kv.del(`chat:${id}`);
await kv.zrem(`user:chat:${userId}`, `chat:${id}`);
2023-05-19 12:33:56 -04:00
revalidatePath("/");
revalidatePath("/chat/[id]");
}