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,
} 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,

View file

@ -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) {

View file

@ -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(

View file

@ -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,12 +15,14 @@ export interface Chat {
}
const Page = () => {
const { user, isLoading: isUserLoading } = useUser();
const [chats, setChats] = useState<Chat[]>([]);
const [loading, setLoading] = useState(true);
useEffect(() => {
const fetchChats = async () => {
setLoading(true);
try {
if (!user) return;
const res = await fetch(`/api/chats`, {
method: 'GET',
@ -28,16 +31,24 @@ const Page = () => {
},
});
const data = await res.json();
if (!res.ok) {
throw new Error('Failed to fetch chats');
}
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 (
<div className="flex flex-row items-center justify-center min-h-screen">
<svg
aria-hidden="true"
@ -56,7 +67,20 @@ const Page = () => {
/>
</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 className="flex flex-col pt-4">
<div className="flex items-center">

View file

@ -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,

View file

@ -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(),