diff --git a/src/app/api/admin/node/route.ts b/src/app/api/admin/node/route.ts index d4a2d2a..2bb50d3 100644 --- a/src/app/api/admin/node/route.ts +++ b/src/app/api/admin/node/route.ts @@ -15,6 +15,14 @@ export async function PATCH(req: NextRequest) { where: eq(nodes.domain, domain), }); + // 2. Fallback: If not found, check if there is exactly ONE node in the system. + if (!node) { + const allNodes = await db.query.nodes.findMany({ limit: 2 }); + if (allNodes.length === 1) { + node = allNodes[0]; + } + } + if (!node) { [node] = await db.insert(nodes).values({ domain, @@ -44,6 +52,8 @@ export async function PATCH(req: NextRequest) { isNsfw: data.isNsfw ?? node.isNsfw, turnstileSiteKey: data.turnstileSiteKey !== undefined ? data.turnstileSiteKey : node.turnstileSiteKey, turnstileSecretKey: data.turnstileSecretKey !== undefined ? data.turnstileSecretKey : node.turnstileSecretKey, + // Fix domain drift: ensure the DB matches the current environment + domain: domain, updatedAt: new Date(), }) .where(eq(nodes.id, node.id)) diff --git a/src/app/api/node/route.ts b/src/app/api/node/route.ts index 7ad2468..e4252fd 100644 --- a/src/app/api/node/route.ts +++ b/src/app/api/node/route.ts @@ -9,10 +9,21 @@ export async function GET() { if (!db) return NextResponse.json({}); const domain = process.env.NEXT_PUBLIC_NODE_DOMAIN || 'localhost:3000'; - const node = await db.query.nodes.findFirst({ + + // 1. Try exact match + let node = await db.query.nodes.findFirst({ where: eq(nodes.domain, domain), }); + // 2. Fallback: If not found, check if there is exactly ONE node in the system. + // This handles upgrades where the env var domain might differ from the DB domain (e.g. localhost vs production). + if (!node) { + const allNodes = await db.query.nodes.findMany({ limit: 2 }); + if (allNodes.length === 1) { + node = allNodes[0]; + } + } + // Ensure we have a public key const publicKey = await getNodePublicKey(); @@ -47,8 +58,8 @@ export async function GET() { }); } - return NextResponse.json({ - ...node, + return NextResponse.json({ + ...node, publicKey, // Always include the public key admins, // Don't expose the secret keys