diff --git a/app/(chat)/chat/[id]/page.tsx b/app/(chat)/chat/[id]/page.tsx index c8de380..bf8775c 100644 --- a/app/(chat)/chat/[id]/page.tsx +++ b/app/(chat)/chat/[id]/page.tsx @@ -23,8 +23,13 @@ export async function generateMetadata({ } const chat = await getChat(params.id, session.user.id) - return { - title: chat?.title.toString().slice(0, 50) ?? 'Chat' + + if (!chat || 'error' in chat) { + redirect('/') + } else { + return { + title: chat?.title.toString().slice(0, 50) ?? 'Chat' + } } } @@ -39,22 +44,22 @@ export default async function ChatPage({ params }: ChatPageProps) { const userId = session.user.id as string const chat = await getChat(params.id, userId) - if (!chat) { + if (!chat || 'error' in chat) { redirect('/') - } + } else { + if (chat?.userId !== session?.user?.id) { + notFound() + } - if (chat?.userId !== session?.user?.id) { - notFound() + return ( + + + + ) } - - return ( - - - - ) } diff --git a/app/actions.ts b/app/actions.ts index c624f56..1398aca 100644 --- a/app/actions.ts +++ b/app/actions.ts @@ -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:${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) { diff --git a/components/sidebar-list.tsx b/components/sidebar-list.tsx index 0a7fb1a..45c2f4a 100644 --- a/components/sidebar-list.tsx +++ b/components/sidebar-list.tsx @@ -2,6 +2,7 @@ import { clearChats, getChats } from '@/app/actions' import { ClearHistory } from '@/components/clear-history' import { SidebarItems } from '@/components/sidebar-items' import { ThemeToggle } from '@/components/theme-toggle' +import { redirect } from 'next/navigation' import { cache } from 'react' interface SidebarListProps { @@ -16,23 +17,27 @@ const loadChats = cache(async (userId?: string) => { export async function SidebarList({ userId }: SidebarListProps) { const chats = await loadChats(userId) - return ( - - - {chats?.length ? ( - - - - ) : ( - - No chat history - - )} + if (!chats || 'error' in chats) { + redirect('/') + } else { + return ( + + + {chats?.length ? ( + + + + ) : ( + + No chat history + + )} + + + + 0} /> + - - - 0} /> - - - ) + ) + } }
No chat history