diff --git a/src/app/api/chat/route.ts b/src/app/api/chat/route.ts index d9f9c6b..4d4fc76 100644 --- a/src/app/api/chat/route.ts +++ b/src/app/api/chat/route.ts @@ -21,6 +21,7 @@ import { getCustomOpenaiModelName, } from '@/lib/config'; import { searchHandlers } from '@/lib/search'; +import { getSession } from '@auth0/nextjs-auth0'; export const runtime = 'nodejs'; export const dynamic = 'force-dynamic'; @@ -133,8 +134,16 @@ const handleHistorySave = async ( focusMode: string, files: string[], ) => { + const session = await getSession(); + if (!session?.user) { + throw new Error('Unauthorized'); + } + const chat = await db.query.chats.findFirst({ - where: eq(chats.id, message.chatId), + where: and( + eq(chats.id, message.chatId), + eq(chats.userId, session.user.sub) + ), }); if (!chat) { @@ -142,6 +151,7 @@ const handleHistorySave = async ( .insert(chats) .values({ id: message.chatId, + userId: session.user.sub, title: message.content, createdAt: new Date().toString(), focusMode: focusMode, diff --git a/src/app/api/chats/[id]/route.ts b/src/app/api/chats/[id]/route.ts index 6891454..be29bbd 100644 --- a/src/app/api/chats/[id]/route.ts +++ b/src/app/api/chats/[id]/route.ts @@ -1,16 +1,25 @@ import db from '@/lib/db'; import { chats, messages } from '@/lib/db/schema'; -import { eq } from 'drizzle-orm'; +import { eq, and } from 'drizzle-orm'; +import { getSession } from '@auth0/nextjs-auth0'; export const GET = async ( req: Request, { params }: { params: Promise<{ id: string }> }, ) => { try { + const session = await getSession(); + if (!session?.user) { + return Response.json({ message: 'Unauthorized' }, { status: 401 }); + } + const { id } = await params; const chatExists = await db.query.chats.findFirst({ - where: eq(chats.id, id), + where: and( + eq(chats.id, id), + eq(chats.userId, session.user.sub) + ), }); if (!chatExists) { @@ -42,10 +51,18 @@ export const DELETE = async ( { params }: { params: Promise<{ id: string }> }, ) => { try { + const session = await getSession(); + if (!session?.user) { + return Response.json({ message: 'Unauthorized' }, { status: 401 }); + } + const { id } = await params; const chatExists = await db.query.chats.findFirst({ - where: eq(chats.id, id), + where: and( + eq(chats.id, id), + eq(chats.userId, session.user.sub) + ), }); if (!chatExists) { diff --git a/src/app/api/chats/route.ts b/src/app/api/chats/route.ts index 986a192..ece0a5a 100644 --- a/src/app/api/chats/route.ts +++ b/src/app/api/chats/route.ts @@ -1,10 +1,21 @@ import db from '@/lib/db'; +import { getSession } from '@auth0/nextjs-auth0'; +import { chats } from '@/lib/db/schema'; // adjust this import to wherever your schema is defined +import { eq } from 'drizzle-orm'; export const GET = async (req: Request) => { try { - let chats = await db.query.chats.findMany(); - chats = chats.reverse(); - return Response.json({ chats: chats }, { status: 200 }); + const session = await getSession(); + if (!session?.user) { + return Response.json({ message: 'Unauthorized' }, { status: 401 }); + } + + let userChats = await db.query.chats.findMany({ + where: eq(chats.userId, session.user.sub), + }); + userChats = userChats.reverse(); + + return Response.json({ chats: userChats }, { status: 200 }); } catch (err) { console.error('Error in getting chats: ', err); return Response.json( diff --git a/src/app/library/page.tsx b/src/app/library/page.tsx index 9c40b2b..40c6aae 100644 --- a/src/app/library/page.tsx +++ b/src/app/library/page.tsx @@ -5,6 +5,7 @@ import { cn, formatTimeDifference } from '@/lib/utils'; import { BookOpenText, ClockIcon, Delete, ScanEye } from 'lucide-react'; import Link from 'next/link'; import { useEffect, useState } from 'react'; +import { useUser } from '@auth0/nextjs-auth0'; export interface Chat { id: string; @@ -14,49 +15,72 @@ export interface Chat { } const Page = () => { + const { user, isLoading: isUserLoading } = useUser(); const [chats, setChats] = useState([]); const [loading, setLoading] = useState(true); useEffect(() => { const fetchChats = async () => { - setLoading(true); + try { + if (!user) return; - const res = await fetch(`/api/chats`, { - method: 'GET', - headers: { - 'Content-Type': 'application/json', - }, - }); + const res = await fetch(`/api/chats`, { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + }, + }); - const data = await res.json(); + if (!res.ok) { + throw new Error('Failed to fetch chats'); + } - setChats(data.chats); - setLoading(false); + const data = await res.json(); + setChats(data.chats); + } catch (error) { + console.error('Error fetching chats:', error); + } finally { + setLoading(false); + } }; fetchChats(); - }, []); + }, [user]); - return loading ? ( -
- -
- ) : ( + if (isUserLoading || loading) { + return ( +
+ +
+ ); + } + + if (!user) { + return ( +
+

+ Please log in to view your chats. +

+
+ ); + } + + return (
diff --git a/src/components/Navbar.tsx b/src/components/Navbar.tsx index 62f0440..2fc4c34 100644 --- a/src/components/Navbar.tsx +++ b/src/components/Navbar.tsx @@ -3,7 +3,6 @@ import { Message } from './ChatWindow'; import { useEffect, useState } from 'react'; import { formatTimeDifference } from '@/lib/utils'; import DeleteChat from './DeleteChat'; -import ProfileButton from './ProfileButton'; // adjust path if needed const Navbar = ({ chatId, diff --git a/src/lib/db/schema.ts b/src/lib/db/schema.ts index cee9660..7b18263 100644 --- a/src/lib/db/schema.ts +++ b/src/lib/db/schema.ts @@ -19,6 +19,7 @@ interface File { export const chats = sqliteTable('chats', { id: text('id').primaryKey(), + userId: text('userId').notNull(), // user id from auth0 title: text('title').notNull(), createdAt: text('createdAt').notNull(), focusMode: text('focusMode').notNull(),