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,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 (
<AI initialAIState={{ chatId: chat.id, messages: chat.messages }}>
<Chat
id={chat.id}
session={session}
initialMessages={chat.messages}
missingKeys={missingKeys}
/>
</AI>
)
}
return (
<AI initialAIState={{ chatId: chat.id, messages: chat.messages }}>
<Chat
id={chat.id}
session={session}
initialMessages={chat.messages}
missingKeys={missingKeys}
/>
</AI>
)
}