chatbot-template/app/actions.ts

28 lines
561 B
TypeScript
Raw Normal View History

2023-06-02 15:33:48 -04:00
'use server'
2023-05-19 12:33:56 -04:00
2023-06-02 15:33:48 -04:00
import { kv } from '@vercel/kv'
import { revalidatePath } from 'next/cache'
2023-05-19 12:33:56 -04:00
2023-06-02 14:28:05 -04:00
export async function removeChat({
id,
path,
2023-06-02 15:33:48 -04:00
userId
2023-06-02 14:28:05 -04:00
}: {
2023-06-02 15:33:48 -04:00
id: string
userId: string
path: string
2023-06-02 14:28:05 -04:00
}) {
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:33:48 -04:00
const uid = await kv.hget<string>(`chat:${id}`, 'userId')
2023-06-02 15:15:35 -04:00
if (uid !== userId) {
2023-06-02 15:33:48 -04:00
throw new Error('Unauthorized')
2023-06-02 15:15:35 -04:00
}
2023-06-02 15:33:48 -04:00
await kv.del(`chat:${id}`)
await kv.zrem(`user:chat:${userId}`, `chat:${id}`)
2023-05-19 12:33:56 -04:00
2023-06-02 15:33:48 -04:00
revalidatePath('/')
revalidatePath('/chat/[id]')
2023-05-19 12:33:56 -04:00
}