From 7f27c90b63a31b0f3a93b87845be45838edc49f5 Mon Sep 17 00:00:00 2001 From: Genuinely Date: Thu, 27 Mar 2025 23:43:17 -0700 Subject: [PATCH] fixed some imports need to reset db in docker --- notes.md | 5 ++++- searxng/settings.yml | 2 +- src/app/api/chat/route.ts | 4 ++-- src/app/api/chats/[id]/route.ts | 6 +++--- src/app/api/chats/route.ts | 36 +++++++++++++++++++++++++++------ src/lib/db/schema.ts | 21 ++++++++++--------- 6 files changed, 51 insertions(+), 23 deletions(-) diff --git a/notes.md b/notes.md index 3303811..d124374 100644 --- a/notes.md +++ b/notes.md @@ -2,4 +2,7 @@ * use `yard add` for new packages and the packages will be updated automatically * auth-secret generate with `openssl rand -hex 32` * need to no-cache if updating packages...don't need to remove `--frozen-lockfile` can be handled with `yarn` -* version 2 is using \ No newline at end of file +* version 2 is using + +### Adding userID to the schema +* complaining about userID column not existing find a way to reset the database within the docker container \ No newline at end of file diff --git a/searxng/settings.yml b/searxng/settings.yml index 54d27c4..fe0d105 100644 --- a/searxng/settings.yml +++ b/searxng/settings.yml @@ -6,7 +6,7 @@ general: search: autocomplete: 'google' formats: - - html + # - html - json server: diff --git a/src/app/api/chat/route.ts b/src/app/api/chat/route.ts index 4d4fc76..96dee71 100644 --- a/src/app/api/chat/route.ts +++ b/src/app/api/chat/route.ts @@ -21,7 +21,7 @@ import { getCustomOpenaiModelName, } from '@/lib/config'; import { searchHandlers } from '@/lib/search'; -import { getSession } from '@auth0/nextjs-auth0'; +import { auth0 } from '@/lib/auth0'; export const runtime = 'nodejs'; export const dynamic = 'force-dynamic'; @@ -134,7 +134,7 @@ const handleHistorySave = async ( focusMode: string, files: string[], ) => { - const session = await getSession(); + const session = await auth0.getSession(); if (!session?.user) { throw new Error('Unauthorized'); } diff --git a/src/app/api/chats/[id]/route.ts b/src/app/api/chats/[id]/route.ts index be29bbd..8dc81a7 100644 --- a/src/app/api/chats/[id]/route.ts +++ b/src/app/api/chats/[id]/route.ts @@ -1,14 +1,14 @@ import db from '@/lib/db'; import { chats, messages } from '@/lib/db/schema'; import { eq, and } from 'drizzle-orm'; -import { getSession } from '@auth0/nextjs-auth0'; +import { auth0 } from '@/lib/auth0'; export const GET = async ( req: Request, { params }: { params: Promise<{ id: string }> }, ) => { try { - const session = await getSession(); + const session = await auth0.getSession(); if (!session?.user) { return Response.json({ message: 'Unauthorized' }, { status: 401 }); } @@ -51,7 +51,7 @@ export const DELETE = async ( { params }: { params: Promise<{ id: string }> }, ) => { try { - const session = await getSession(); + const session = await auth0.getSession(); if (!session?.user) { return Response.json({ message: 'Unauthorized' }, { status: 401 }); } diff --git a/src/app/api/chats/route.ts b/src/app/api/chats/route.ts index ece0a5a..9d3db62 100644 --- a/src/app/api/chats/route.ts +++ b/src/app/api/chats/route.ts @@ -1,26 +1,50 @@ import db from '@/lib/db'; -import { getSession } from '@auth0/nextjs-auth0'; +import { auth0 } from '@/lib/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 { - const session = await getSession(); + // Log that we're starting the request + console.log('Starting /api/chats GET request'); + + const session = await auth0.getSession(); + console.log('Session status:', session ? 'exists' : 'null'); + if (!session?.user) { - return Response.json({ message: 'Unauthorized' }, { status: 401 }); + console.log('No session found, returning 401'); + return Response.json( + { message: 'Unauthorized - Please log in' }, + { status: 401 } + ); } + console.log('User ID:', session.user.sub); + + // Log the database query attempt + console.log('Attempting database query'); let userChats = await db.query.chats.findMany({ where: eq(chats.userId, session.user.sub), }); + console.log('Query successful, found', userChats.length, 'chats'); + userChats = userChats.reverse(); return Response.json({ chats: userChats }, { status: 200 }); } catch (err) { - console.error('Error in getting chats: ', err); + // Log the full error details + console.error('Detailed error in getting chats:', { + error: err, + message: err instanceof Error ? err.message : 'Unknown error', + stack: err instanceof Error ? err.stack : undefined + }); + return Response.json( - { message: 'An error has occurred.' }, - { status: 500 }, + { + message: 'An error has occurred while fetching chats.', + details: err instanceof Error ? err.message : 'Unknown error' + }, + { status: 500 } ); } }; diff --git a/src/lib/db/schema.ts b/src/lib/db/schema.ts index 7b18263..4ae971f 100644 --- a/src/lib/db/schema.ts +++ b/src/lib/db/schema.ts @@ -1,6 +1,17 @@ import { sql } from 'drizzle-orm'; import { text, integer, sqliteTable } from 'drizzle-orm/sqlite-core'; +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(), + files: text('files', { mode: 'json' }) + .$type() + .default(sql`'[]'`), +}); + export const messages = sqliteTable('messages', { id: integer('id').primaryKey(), content: text('content').notNull(), @@ -17,13 +28,3 @@ interface File { fileId: string; } -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(), - files: text('files', { mode: 'json' }) - .$type() - .default(sql`'[]'`), -});