Check session user id before performing action (#391)
This commit is contained in:
parent
c2757f87f9
commit
b38c49da59
3 changed files with 62 additions and 36 deletions
|
|
@ -8,10 +8,18 @@ import { auth } from '@/auth'
|
|||
import { type Chat } from '@/lib/types'
|
||||
|
||||
export async function getChats(userId?: string | null) {
|
||||
const session = await auth()
|
||||
|
||||
if (!userId) {
|
||||
return []
|
||||
}
|
||||
|
||||
if (userId !== session?.user?.id) {
|
||||
return {
|
||||
error: 'Unauthorized'
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const pipeline = kv.pipeline()
|
||||
const chats: string[] = await kv.zrange(`user:chat:${userId}`, 0, -1, {
|
||||
|
|
@ -31,6 +39,14 @@ export async function getChats(userId?: string | null) {
|
|||
}
|
||||
|
||||
export async function getChat(id: string, userId: string) {
|
||||
const session = await auth()
|
||||
|
||||
if (userId !== session?.user?.id) {
|
||||
return {
|
||||
error: 'Unauthorized'
|
||||
}
|
||||
}
|
||||
|
||||
const chat = await kv.hgetall<Chat>(`chat:${id}`)
|
||||
|
||||
if (!chat || (userId && chat.userId !== userId)) {
|
||||
|
|
@ -49,7 +65,7 @@ export async function removeChat({ id, path }: { id: string; path: string }) {
|
|||
}
|
||||
}
|
||||
|
||||
//Convert uid to string for consistent comparison with session.user.id
|
||||
// Convert uid to string for consistent comparison with session.user.id
|
||||
const uid = String(await kv.hget(`chat:${id}`, 'userId'))
|
||||
|
||||
if (uid !== session?.user?.id) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue