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,12 +15,14 @@ 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',
@ -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); setChats(data.chats);
} catch (error) {
console.error('Error fetching chats:', error);
} finally {
setLoading(false); setLoading(false);
}
}; };
fetchChats(); fetchChats();
}, []); }, [user]);
return loading ? ( if (isUserLoading || loading) {
return (
<div className="flex flex-row items-center justify-center min-h-screen"> <div className="flex flex-row items-center justify-center min-h-screen">
<svg <svg
aria-hidden="true" aria-hidden="true"
@ -56,7 +67,20 @@ const Page = () => {
/> />
</svg> </svg>
</div> </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(),