An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
refactor: make userId required param to prevent auth duplication
Yoshiki Miurad61aee67abad
diff --git a/app/search/[id]/page.tsx b/app/search/[id]/page.tsxindex 29072957..f96a8bc2 100644--- a/app/search/[id]/page.tsx+++ b/app/search/[id]/page.tsx@@ -2,7 +2,7 @@ import { notFound, redirect } from 'next/navigation'import { UIMessage } from 'ai'-import { getChat } from '@/lib/actions/chat'+import { loadChat } from '@/lib/actions/chat'import { getCurrentUserId } from '@/lib/auth/get-current-user'import { getModels } from '@/lib/config/models'@@ -16,7 +16,7 @@ export async function generateMetadata(props: {const { id } = await props.paramsconst userId = await getCurrentUserId()- const chat = await getChat(id, userId)+ const chat = await loadChat(id, userId)if (!chat) {return { title: 'Search' }@@ -33,7 +33,7 @@ export default async function SearchPage(props: {const { id } = await props.paramsconst userId = await getCurrentUserId()- const chat = await getChat(id, userId)+ const chat = await loadChat(id, userId)if (!chat) {notFound()diff --git a/lib/actions/chat.ts b/lib/actions/chat.tsindex 053a75e5..7ed10bfc 100644--- a/lib/actions/chat.ts+++ b/lib/actions/chat.ts@@ -35,17 +35,6 @@ export async function getChatsPage(limit = 20, offset = 0) {return dbActions.getChatsPage(userId, limit, offset)}-/**- * Get a chat with messages (no cache)- * @deprecated Use loadChat instead- */-export async function getChat(- chatId: string,- requestingUserId?: string-): Promise<(Chat & { messages: UIMessage[] }) | null> {- return loadChat(chatId, requestingUserId)-}-/*** Load a chat with messages* If requestingUserId is provided, it will be used for authorization@@ -61,18 +50,13 @@ export async function loadChat(/*** Create a new chat+ * @param userId - Required. Pass userId to avoid duplicate auth calls*/export async function createChat(- id?: string,- title?: string,- userId?: string+ id: string | undefined,+ title: string | undefined,+ userId: string): Promise<Chat> {- // If userId is not provided, get it from auth- const uid = userId ?? (await getCurrentUserId())- if (!uid) {- throw new Error('User not authenticated')- }-const chatId = id || generateId()const chatTitle = title || DEFAULT_CHAT_TITLE@@ -80,7 +64,7 @@ export async function createChat(const chat = await dbActions.createChat({id: chatId,title: chatTitle.substring(0, 255),- userId: uid,+ userId,visibility: 'private'})@@ -92,16 +76,14 @@ export async function createChat(}/**- * Create a new chat and save the first message+ * Create a new chat and save the first message (public API with auth)*/export async function createChatAndSaveMessage(message: UIMessage,- title?: string,- userId?: string+ title?: string): Promise<{ chat: Chat; message: Message }> {- // If userId is not provided, get it from auth- const uid = userId ?? (await getCurrentUserId())- if (!uid) {+ const userId = await getCurrentUserId()+ if (!userId) {throw new Error('User not authenticated')}@@ -116,7 +98,7 @@ export async function createChatAndSaveMessage(const chat = await dbActions.createChat({id: chatId,title: chatTitle.substring(0, 255),- userId: uid,+ userId,visibility: 'private'})@@ -168,33 +150,18 @@ export async function createChatWithFirstMessage(return { chat, message: dbMessage }}-/**- * Upsert a message (insert or update)- * @deprecated Use upsertMessage instead- */-export async function saveMessage(- chatId: string,- message: UIMessage-): Promise<Message> {- return upsertMessage(chatId, message)-}/*** Upsert a message to a chat+ * @param userId - Required. Pass userId to avoid duplicate auth calls*/export async function upsertMessage(chatId: string,message: UIMessage,- userId?: string+ userId: string): Promise<Message> {- // If userId is not provided, get it from auth- const uid = userId ?? (await getCurrentUserId())- if (!uid) {- throw new Error('User not authenticated')- }-// Verify access- const chat = await dbActions.getChat(chatId, uid)+ const chat = await dbActions.getChat(chatId, userId)if (!chat) {throw new Error('Chat not found or unauthorized')}@@ -212,6 +179,7 @@ export async function upsertMessage(return dbMessage}+/*** Delete a chat*/diff --git a/lib/streaming/message-preparation.ts b/lib/streaming/message-preparation.tsindex 8182a898..445d0b3b 100644--- a/lib/streaming/message-preparation.ts+++ b/lib/streaming/message-preparation.ts@@ -46,7 +46,7 @@ export async function prepareMessagesForRegeneration(} else {// If it's a user message that was edited, save the updated message firstif (message && message.id === messageId) {- await upsertMessage(chatId, message)+ await upsertMessage(chatId, message, userId)}// Delete everything after this user messageconst messagesToDelete = currentChat.messages.slice(messageIndex + 1)@@ -90,10 +90,10 @@ export async function prepareMessagesForSubmission(// If chat doesn't exist, create it with a temporary titleif (!chat) {- await createChat(chatId, DEFAULT_CHAT_TITLE)+ await createChat(chatId, DEFAULT_CHAT_TITLE, userId)}- await upsertMessage(chatId, messageWithId)+ await upsertMessage(chatId, messageWithId, userId)// Get all messages including the one just savedconst updatedChat = await loadChat(chatId, userId)