Skip to content

An AI-powered search engine with a generative UI

feat: Make userId required for all DB operations

Yoshiki Miuradabfddd94c32
diff --git a/lib/actions/chat-db.ts b/lib/actions/chat-db.ts
index f4a367b9..ea93821f 100644
--- a/lib/actions/chat-db.ts
+++ b/lib/actions/chat-db.ts
@@ -3,7 +3,7 @@
import * as chatDb from '@/lib/db/chat'
// Get all chats for a user
-export async function getChats(userId?: string | null) {
+export async function getChats(userId: string) {
return chatDb.getChats(userId)
}
@@ -13,22 +13,22 @@ export async function getChatsPage(userId: string, limit = 20, offset = 0) {
}
// Get a single chat by ID
-export async function getChat(id: string, userId: string = 'anonymous') {
+export async function getChat(id: string, userId: string) {
return chatDb.getChat(id, userId)
}
// Clear all chats for a user
-export async function clearChats(userId: string = 'anonymous') {
+export async function clearChats(userId: string) {
return chatDb.clearChats(userId)
}
// Delete a single chat
-export async function deleteChat(chatId: string, userId = 'anonymous') {
+export async function deleteChat(chatId: string, userId: string) {
return chatDb.deleteChat(chatId, userId)
}
// Save a chat (with message history)
-export async function saveChat(chat: any, userId: string = 'anonymous') {
+export async function saveChat(chat: any, userId: string) {
try {
// First save the chat
const savedChat = await chatDb.saveChat(chat, userId)
@@ -79,6 +79,6 @@ export async function getSharedChat(id: string) {
}
// Share a chat
-export async function shareChat(id: string, userId: string = 'anonymous') {
+export async function shareChat(id: string, userId: string) {
return chatDb.shareChat(id, userId)
}
diff --git a/lib/db/chat.ts b/lib/db/chat.ts
index 59a56a04..7effad02 100644
--- a/lib/db/chat.ts
+++ b/lib/db/chat.ts
@@ -29,17 +29,12 @@ export async function createChat({
// Get a chat by ID
export async function getChat(
id: string,
- userId?: string
+ userId: string
): Promise<Chat | null> {
const [chat] = await db.select().from(chats).where(eq(chats.id, id)).limit(1)
// If userId is provided, check if the chat belongs to the user or is public
- if (
- userId &&
- chat &&
- chat.userId !== userId &&
- chat.visibility !== 'public'
- ) {
+ if (chat && chat.userId !== userId && chat.visibility !== 'public') {
return null // User doesn't have access to this chat
}
@@ -47,11 +42,7 @@ export async function getChat(
}
// Get all chats for a user
-export async function getChats(userId?: string | null): Promise<Chat[]> {
- if (!userId) {
- return []
- }
-
+export async function getChats(userId: string): Promise<Chat[]> {
return db
.select()
.from(chats)
@@ -106,24 +97,23 @@ export async function updateChat(
// Delete a chat and its messages
export async function deleteChat(
id: string,
- userId = 'anonymous'
+ userId: string
): Promise<{ error?: string }> {
try {
// First verify the chat exists and belongs to the user
- const chat = await getChat(id)
+ const chat = await getChat(id, userId)
if (!chat) {
console.warn(`Attempted to delete non-existent chat: ${id}`)
return { error: 'Chat not found' }
}
- // Optional authorization check
- // if (userId !== 'anonymous' && chat.userId !== userId) {
- // return { error: 'Unauthorized' }
- // }
+ // Check if the chat belongs to the user
+ if (chat.userId !== userId) {
+ return { error: 'Unauthorized' }
+ }
- // First delete all messages for this chat (this happens automatically due to ON DELETE CASCADE)
- // Then delete the chat
+ // Delete the chat
await db.delete(chats).where(eq(chats.id, id))
// Revalidate the root path where the chat history is displayed
@@ -168,9 +158,7 @@ export async function getChatMessages(chatId: string): Promise<Message[]> {
}
// Clear all chats for a user
-export async function clearChats(
- userId: string = 'anonymous'
-): Promise<{ error?: string }> {
+export async function clearChats(userId: string): Promise<{ error?: string }> {
try {
// Get all chat IDs for this user
const userChats = await getChats(userId)
@@ -179,8 +167,6 @@ export async function clearChats(
return { error: 'No chats to clear' }
}
- const chatIds = userChats.map(chat => chat.id)
-
// Delete all chats for this user
// Message deletion happens automatically due to ON DELETE CASCADE
await db.delete(chats).where(eq(chats.userId, userId))
@@ -194,10 +180,10 @@ export async function clearChats(
}
// Save a chat (create or update)
-export async function saveChat(chat: Chat, userId: string = 'anonymous') {
+export async function saveChat(chat: Chat, userId: string) {
try {
// Check if the chat exists
- const existingChat = await getChat(chat.id)
+ const existingChat = await getChat(chat.id, userId)
if (existingChat) {
// Update existing chat
@@ -231,7 +217,12 @@ export async function saveChat(chat: Chat, userId: string = 'anonymous') {
// Get a shared chat
export async function getSharedChat(id: string) {
try {
- const chat = await getChat(id)
+ // For shared chats, we bypass the userId check and directly query the database
+ const [chat] = await db
+ .select()
+ .from(chats)
+ .where(eq(chats.id, id))
+ .limit(1)
if (!chat || chat.visibility !== 'public') {
return null
@@ -245,9 +236,9 @@ export async function getSharedChat(id: string) {
}
// Share a chat (make it public)
-export async function shareChat(id: string, userId: string = 'anonymous') {
+export async function shareChat(id: string, userId: string) {
try {
- const chat = await getChat(id)
+ const chat = await getChat(id, userId)
if (!chat || chat.userId !== userId) {
return null