Fix chat panel token fetch: use /api/workspaces not URL slug

URL param 'mark-account' != workspace slug 'mark'. Fetch default token
from /api/workspaces?include_default_token=true which resolves the real
slug server-side.

Made-with: Cursor
This commit is contained in:
2026-04-27 16:26:55 -07:00
parent 56e7c2fb5c
commit 210fba4e08
2 changed files with 29 additions and 5 deletions

View File

@@ -10,7 +10,7 @@ import { NextResponse } from 'next/server';
import { authSession } from '@/lib/auth/session-server';
import { queryOne } from '@/lib/db-postgres';
import { ensureWorkspaceForUser, listWorkspacesForUser } from '@/lib/workspaces';
import { requireWorkspacePrincipal } from '@/lib/auth/workspace-auth';
import { requireWorkspacePrincipal, listWorkspaceApiKeys, mintWorkspaceApiKey, revealWorkspaceApiKey } from '@/lib/auth/workspace-auth';
export async function GET(request: Request) {
if (request.headers.get('authorization')?.toLowerCase().startsWith('bearer vibn_sk_')) {
@@ -49,6 +49,29 @@ export async function GET(request: Request) {
}
}
const url = new URL(request.url);
const includeDefaultToken = url.searchParams.get('include_default_token') === 'true';
if (includeDefaultToken && list.length > 0) {
const ws = list[0];
let defaultToken: string | null = null;
try {
const keys = await listWorkspaceApiKeys(ws.id);
let defaultKey = keys.find((k: any) => k.name === 'default' && !k.revoked_at);
if (!defaultKey) {
const minted = await mintWorkspaceApiKey({ workspaceId: ws.id, name: 'default', createdBy: userRow!.id, scopes: ['workspace:*'] });
defaultToken = minted.token;
} else {
defaultToken = await revealWorkspaceApiKey(ws.id, defaultKey.id);
if (!defaultToken) {
const minted = await mintWorkspaceApiKey({ workspaceId: ws.id, name: 'default', createdBy: userRow!.id, scopes: ['workspace:*'] });
defaultToken = minted.token;
}
}
} catch { /* non-fatal */ }
return NextResponse.json({ workspaces: list.map(serializeWorkspace), defaultToken });
}
return NextResponse.json({ workspaces: list.map(serializeWorkspace) });
}