working attempt to add user id auth0 assosciate with each chat NOT WORKING currently

This commit is contained in:
Genuinely 2025-03-27 17:00:35 -07:00
parent 7add89b205
commit c2306f9d20
6 changed files with 101 additions and 39 deletions

View file

@ -21,6 +21,7 @@ import {
getCustomOpenaiModelName, getCustomOpenaiModelName,
} from '@/lib/config'; } from '@/lib/config';
import { searchHandlers } from '@/lib/search'; import { searchHandlers } from '@/lib/search';
import { getSession } from '@auth0/nextjs-auth0';
export const runtime = 'nodejs'; export const runtime = 'nodejs';
export const dynamic = 'force-dynamic'; export const dynamic = 'force-dynamic';
@ -133,8 +134,16 @@ const handleHistorySave = async (
focusMode: string, focusMode: string,
files: string[], files: string[],
) => { ) => {
const session = await getSession();
if (!session?.user) {
throw new Error('Unauthorized');
}
const chat = await db.query.chats.findFirst({ 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) { if (!chat) {
@ -142,6 +151,7 @@ const handleHistorySave = async (
.insert(chats) .insert(chats)
.values({ .values({
id: message.chatId, id: message.chatId,
userId: session.user.sub,
title: message.content, title: message.content,
createdAt: new Date().toString(), createdAt: new Date().toString(),
focusMode: focusMode, focusMode: focusMode,

View file

@ -1,16 +1,25 @@
import db from '@/lib/db'; import db from '@/lib/db';
import { chats, messages } from '@/lib/db/schema'; 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 ( export const GET = async (
req: Request, req: Request,
{ params }: { params: Promise<{ id: string }> }, { params }: { params: Promise<{ id: string }> },
) => { ) => {
try { try {
const session = await getSession();
if (!session?.user) {
return Response.json({ message: 'Unauthorized' }, { status: 401 });
}
const { id } = await params; const { id } = await params;
const chatExists = await db.query.chats.findFirst({ 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) { if (!chatExists) {
@ -42,10 +51,18 @@ export const DELETE = async (
{ params }: { params: Promise<{ id: string }> }, { params }: { params: Promise<{ id: string }> },
) => { ) => {
try { try {
const session = await getSession();
if (!session?.user) {
return Response.json({ message: 'Unauthorized' }, { status: 401 });
}
const { id } = await params; const { id } = await params;
const chatExists = await db.query.chats.findFirst({ 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) { if (!chatExists) {

View file

@ -1,10 +1,21 @@
import db from '@/lib/db'; 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) => { export const GET = async (req: Request) => {
try { try {
let chats = await db.query.chats.findMany(); const session = await getSession();
chats = chats.reverse(); if (!session?.user) {
return Response.json({ chats: chats }, { status: 200 }); 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) { } catch (err) {
console.error('Error in getting chats: ', err); console.error('Error in getting chats: ', err);
return Response.json( return Response.json(

View file

@ -5,6 +5,7 @@ import { cn, formatTimeDifference } from '@/lib/utils';
import { BookOpenText, ClockIcon, Delete, ScanEye } from 'lucide-react'; import { BookOpenText, ClockIcon, Delete, ScanEye } from 'lucide-react';
import Link from 'next/link'; import Link from 'next/link';
import { useEffect, useState } from 'react'; import { useEffect, useState } from 'react';
import { useUser } from '@auth0/nextjs-auth0';
export interface Chat { export interface Chat {
id: string; id: string;
@ -14,49 +15,72 @@ export interface Chat {
} }
const Page = () => { const Page = () => {
const { user, isLoading: isUserLoading } = useUser();
const [chats, setChats] = useState<Chat[]>([]); const [chats, setChats] = useState<Chat[]>([]);
const [loading, setLoading] = useState(true); const [loading, setLoading] = useState(true);
useEffect(() => { useEffect(() => {
const fetchChats = async () => { const fetchChats = async () => {
setLoading(true); try {
if (!user) return;
const res = await fetch(`/api/chats`, { const res = await fetch(`/api/chats`, {
method: 'GET', method: 'GET',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}, },
}); });
const data = await res.json(); if (!res.ok) {
throw new Error('Failed to fetch chats');
}
setChats(data.chats); const data = await res.json();
setLoading(false); setChats(data.chats);
} catch (error) {
console.error('Error fetching chats:', error);
} finally {
setLoading(false);
}
}; };
fetchChats(); fetchChats();
}, []); }, [user]);
return loading ? ( if (isUserLoading || loading) {
<div className="flex flex-row items-center justify-center min-h-screen"> return (
<svg <div className="flex flex-row items-center justify-center min-h-screen">
aria-hidden="true" <svg
className="w-8 h-8 text-light-200 fill-light-secondary dark:text-[#202020] animate-spin dark:fill-[#ffffff3b]" aria-hidden="true"
viewBox="0 0 100 101" className="w-8 h-8 text-light-200 fill-light-secondary dark:text-[#202020] animate-spin dark:fill-[#ffffff3b]"
fill="none" viewBox="0 0 100 101"
xmlns="http://www.w3.org/2000/svg" fill="none"
> xmlns="http://www.w3.org/2000/svg"
<path >
d="M100 50.5908C100.003 78.2051 78.1951 100.003 50.5908 100C22.9765 99.9972 0.997224 78.018 1 50.4037C1.00281 22.7993 22.8108 0.997224 50.4251 1C78.0395 1.00281 100.018 22.8108 100 50.4251ZM9.08164 50.594C9.06312 73.3997 27.7909 92.1272 50.5966 92.1457C73.4023 92.1642 92.1298 73.4365 92.1483 50.6308C92.1669 27.8251 73.4392 9.0973 50.6335 9.07878C27.8278 9.06026 9.10003 27.787 9.08164 50.594Z" <path
fill="currentColor" d="M100 50.5908C100.003 78.2051 78.1951 100.003 50.5908 100C22.9765 99.9972 0.997224 78.018 1 50.4037C1.00281 22.7993 22.8108 0.997224 50.4251 1C78.0395 1.00281 100.018 22.8108 100 50.4251ZM9.08164 50.594C9.06312 73.3997 27.7909 92.1272 50.5966 92.1457C73.4023 92.1642 92.1298 73.4365 92.1483 50.6308C92.1669 27.8251 73.4392 9.0973 50.6335 9.07878C27.8278 9.06026 9.10003 27.787 9.08164 50.594Z"
/> fill="currentColor"
<path />
d="M93.9676 39.0409C96.393 38.4037 97.8624 35.9116 96.9801 33.5533C95.1945 28.8227 92.871 24.3692 90.0681 20.348C85.6237 14.1775 79.4473 9.36872 72.0454 6.45794C64.6435 3.54717 56.3134 2.65431 48.3133 3.89319C45.869 4.27179 44.3768 6.77534 45.014 9.20079C45.6512 11.6262 48.1343 13.0956 50.5786 12.717C56.5073 11.8281 62.5542 12.5399 68.0406 14.7911C73.527 17.0422 78.2187 20.7487 81.5841 25.4923C83.7976 28.5886 85.4467 32.059 86.4416 35.7474C87.1273 38.1189 89.5423 39.6781 91.9676 39.0409Z" <path
fill="currentFill" d="M93.9676 39.0409C96.393 38.4037 97.8624 35.9116 96.9801 33.5533C95.1945 28.8227 92.871 24.3692 90.0681 20.348C85.6237 14.1775 79.4473 9.36872 72.0454 6.45794C64.6435 3.54717 56.3134 2.65431 48.3133 3.89319C45.869 4.27179 44.3768 6.77534 45.014 9.20079C45.6512 11.6262 48.1343 13.0956 50.5786 12.717C56.5073 11.8281 62.5542 12.5399 68.0406 14.7911C73.527 17.0422 78.2187 20.7487 81.5841 25.4923C83.7976 28.5886 85.4467 32.059 86.4416 35.7474C87.1273 38.1189 89.5423 39.6781 91.9676 39.0409Z"
/> fill="currentFill"
</svg> />
</div> </svg>
) : ( </div>
);
}
if (!user) {
return (
<div className="flex flex-row items-center justify-center min-h-screen">
<p className="text-black/70 dark:text-white/70 text-sm">
Please log in to view your chats.
</p>
</div>
);
}
return (
<div> <div>
<div className="flex flex-col pt-4"> <div className="flex flex-col pt-4">
<div className="flex items-center"> <div className="flex items-center">

View file

@ -3,7 +3,6 @@ import { Message } from './ChatWindow';
import { useEffect, useState } from 'react'; import { useEffect, useState } from 'react';
import { formatTimeDifference } from '@/lib/utils'; import { formatTimeDifference } from '@/lib/utils';
import DeleteChat from './DeleteChat'; import DeleteChat from './DeleteChat';
import ProfileButton from './ProfileButton'; // adjust path if needed
const Navbar = ({ const Navbar = ({
chatId, chatId,

View file

@ -19,6 +19,7 @@ interface File {
export const chats = sqliteTable('chats', { export const chats = sqliteTable('chats', {
id: text('id').primaryKey(), id: text('id').primaryKey(),
userId: text('userId').notNull(), // user id from auth0
title: text('title').notNull(), title: text('title').notNull(),
createdAt: text('createdAt').notNull(), createdAt: text('createdAt').notNull(),
focusMode: text('focusMode').notNull(), focusMode: text('focusMode').notNull(),