- chat-context.ts: session history now from fs_sessions - /api/sessions: reads from fs_sessions (NextAuth session auth) - /api/github/connect: NextAuth session + stores in fs_users.data - /api/user/api-key: NextAuth session + stores in fs_users.data - /api/projects/[id]/vision: PATCH to fs_projects JSONB - /api/projects/[id]/knowledge/items: reads from fs_knowledge_items - /api/projects/[id]/knowledge/import-ai-chat: uses pg createKnowledgeItem - lib/server/knowledge.ts: fully rewritten to use PostgreSQL - entrypoint.sh: add fs_knowledge_items and chat_conversations tables Made-with: Cursor
42 lines
1.4 KiB
TypeScript
42 lines
1.4 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import { getServerSession } from 'next-auth';
|
|
import { authOptions } from '@/lib/auth/authOptions';
|
|
import { query } from '@/lib/db-postgres';
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
|
|
export async function GET(request: Request) {
|
|
try {
|
|
const session = await getServerSession(authOptions);
|
|
if (!session?.user?.email) {
|
|
return NextResponse.json({ error: 'No authorization token provided' }, { status: 401 });
|
|
}
|
|
|
|
const rows = await query<{ data: any }>(
|
|
`SELECT data FROM fs_users WHERE data->>'email' = $1 LIMIT 1`,
|
|
[session.user.email]
|
|
);
|
|
|
|
if (rows.length > 0 && rows[0].data?.apiKey) {
|
|
return NextResponse.json({ apiKey: rows[0].data.apiKey });
|
|
}
|
|
|
|
// Generate new API key and store it
|
|
const apiKey = `vibn_${uuidv4().replace(/-/g, '')}`;
|
|
|
|
await query(
|
|
`UPDATE fs_users
|
|
SET data = data || $1::jsonb, updated_at = NOW()
|
|
WHERE data->>'email' = $2`,
|
|
[JSON.stringify({ apiKey, apiKeyCreatedAt: new Date().toISOString() }), session.user.email]
|
|
);
|
|
|
|
return NextResponse.json({ apiKey, isNew: true });
|
|
} catch (error) {
|
|
console.error('[API] Error getting/creating API key:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Failed to get API key', details: error instanceof Error ? error.message : String(error) },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|