feat: Improve node identification logic to handle domain mismatches and update the node's domain during admin updates.

This commit is contained in:
Christopher
2026-01-27 18:48:06 -08:00
parent f59625c83f
commit 4627917921
2 changed files with 24 additions and 3 deletions
+10
View File
@@ -15,6 +15,14 @@ export async function PATCH(req: NextRequest) {
where: eq(nodes.domain, domain), 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) { if (!node) {
[node] = await db.insert(nodes).values({ [node] = await db.insert(nodes).values({
domain, domain,
@@ -44,6 +52,8 @@ export async function PATCH(req: NextRequest) {
isNsfw: data.isNsfw ?? node.isNsfw, isNsfw: data.isNsfw ?? node.isNsfw,
turnstileSiteKey: data.turnstileSiteKey !== undefined ? data.turnstileSiteKey : node.turnstileSiteKey, turnstileSiteKey: data.turnstileSiteKey !== undefined ? data.turnstileSiteKey : node.turnstileSiteKey,
turnstileSecretKey: data.turnstileSecretKey !== undefined ? data.turnstileSecretKey : node.turnstileSecretKey, turnstileSecretKey: data.turnstileSecretKey !== undefined ? data.turnstileSecretKey : node.turnstileSecretKey,
// Fix domain drift: ensure the DB matches the current environment
domain: domain,
updatedAt: new Date(), updatedAt: new Date(),
}) })
.where(eq(nodes.id, node.id)) .where(eq(nodes.id, node.id))
+14 -3
View File
@@ -9,10 +9,21 @@ export async function GET() {
if (!db) return NextResponse.json({}); if (!db) return NextResponse.json({});
const domain = process.env.NEXT_PUBLIC_NODE_DOMAIN || 'localhost:3000'; 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), 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 // Ensure we have a public key
const publicKey = await getNodePublicKey(); const publicKey = await getNodePublicKey();
@@ -47,8 +58,8 @@ export async function GET() {
}); });
} }
return NextResponse.json({ return NextResponse.json({
...node, ...node,
publicKey, // Always include the public key publicKey, // Always include the public key
admins, admins,
// Don't expose the secret keys // Don't expose the secret keys