Check session user id before performing action (#391)

This commit is contained in:
Jeremy 2024-08-01 14:33:37 +05:30 committed by GitHub
parent c2757f87f9
commit b38c49da59
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 62 additions and 36 deletions

View file

@ -23,9 +23,14 @@ export async function generateMetadata({
} }
const chat = await getChat(params.id, session.user.id) const chat = await getChat(params.id, session.user.id)
if (!chat || 'error' in chat) {
redirect('/')
} else {
return { return {
title: chat?.title.toString().slice(0, 50) ?? 'Chat' title: chat?.title.toString().slice(0, 50) ?? 'Chat'
} }
}
} }
export default async function ChatPage({ params }: ChatPageProps) { export default async function ChatPage({ params }: ChatPageProps) {
@ -39,10 +44,9 @@ export default async function ChatPage({ params }: ChatPageProps) {
const userId = session.user.id as string const userId = session.user.id as string
const chat = await getChat(params.id, userId) const chat = await getChat(params.id, userId)
if (!chat) { if (!chat || 'error' in chat) {
redirect('/') redirect('/')
} } else {
if (chat?.userId !== session?.user?.id) { if (chat?.userId !== session?.user?.id) {
notFound() notFound()
} }
@ -57,4 +61,5 @@ export default async function ChatPage({ params }: ChatPageProps) {
/> />
</AI> </AI>
) )
}
} }

View file

@ -8,10 +8,18 @@ import { auth } from '@/auth'
import { type Chat } from '@/lib/types' import { type Chat } from '@/lib/types'
export async function getChats(userId?: string | null) { export async function getChats(userId?: string | null) {
const session = await auth()
if (!userId) { if (!userId) {
return [] return []
} }
if (userId !== session?.user?.id) {
return {
error: 'Unauthorized'
}
}
try { try {
const pipeline = kv.pipeline() const pipeline = kv.pipeline()
const chats: string[] = await kv.zrange(`user:chat:${userId}`, 0, -1, { 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) { 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}`) const chat = await kv.hgetall<Chat>(`chat:${id}`)
if (!chat || (userId && chat.userId !== userId)) { 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')) const uid = String(await kv.hget(`chat:${id}`, 'userId'))
if (uid !== session?.user?.id) { if (uid !== session?.user?.id) {

View file

@ -2,6 +2,7 @@ import { clearChats, getChats } from '@/app/actions'
import { ClearHistory } from '@/components/clear-history' import { ClearHistory } from '@/components/clear-history'
import { SidebarItems } from '@/components/sidebar-items' import { SidebarItems } from '@/components/sidebar-items'
import { ThemeToggle } from '@/components/theme-toggle' import { ThemeToggle } from '@/components/theme-toggle'
import { redirect } from 'next/navigation'
import { cache } from 'react' import { cache } from 'react'
interface SidebarListProps { interface SidebarListProps {
@ -16,6 +17,9 @@ const loadChats = cache(async (userId?: string) => {
export async function SidebarList({ userId }: SidebarListProps) { export async function SidebarList({ userId }: SidebarListProps) {
const chats = await loadChats(userId) const chats = await loadChats(userId)
if (!chats || 'error' in chats) {
redirect('/')
} else {
return ( return (
<div className="flex flex-1 flex-col overflow-hidden"> <div className="flex flex-1 flex-col overflow-hidden">
<div className="flex-1 overflow-auto"> <div className="flex-1 overflow-auto">
@ -35,4 +39,5 @@ export async function SidebarList({ userId }: SidebarListProps) {
</div> </div>
</div> </div>
) )
}
} }