Connect Supabase first: pick or make a project, and only the secret key is pasted

Signs in to the whole Supabase account, lists its projects (one is used straight away), or makes a free one called broke,
then fills in the address and public key and creates the tables. The paste-all-three route stays as the fallback.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
minimus7andClaude Opus 5.5 committed 2026-11-09 11:04:06 -06:00
1 parent 73208b79df
commit 0e1a05801c
9 files changed
+237 -20

No files matched your search

+14 -1
View File
@@ -1,6 +1,8 @@
import { NextResponse } from "next/server";
import { migrate } from "@/lib/migrate";
import { finishConnect } from "@/lib/supabase-mcp";
import { currentEnv } from "@/lib/setup";
import { accountConnected, finishConnect, listProjects } from "@/lib/supabase-mcp";
import { fillFrom } from "@/lib/supabase-fill";
// Supabase sends the browser back here after "Approve". Finish the sign-in, then bring the tables up to date.
// The result goes back as a code (DB_MESSAGES on the Setup page), never as text, so the link can't carry a message.
@@ -15,6 +17,17 @@ export async function GET(request: Request) {
console.error("[broke] supabase callback:", e instanceof Error ? e.message : e);
return back(e instanceof Error && /didn't start/.test(e.message) ? "elsewhere" : "rejected");
}
// Signed in to the whole account with no project set yet: one project is used straight away, several are picked on the page.
if (accountConnected() && !currentEnv().NEXT_PUBLIC_SUPABASE_URL?.includes(".supabase.co")) {
try {
const projects = await listProjects();
if (projects.length !== 1) return back(projects.length ? "pick" : "no-project");
return back((await fillFrom(projects[0].id)).tables ? "filled" : "filled-no-tables");
} catch (e) {
console.error("[broke] supabase fill:", e instanceof Error ? e.message : e);
return back("pick");
}
}
const r = await migrate();
return back(r.error ? "update-failed" : r.applied.length ? "updated" : "current");
}
+2 -2
View File
@@ -4,12 +4,12 @@ import { refFromUrl, startConnect } from "@/lib/supabase-mcp";
const LOOPBACK = /^(localhost|127\.0\.0\.1|\[::1\])(:\d+)?$/;
// "Connect Supabase": returns the Supabase approval page for this project. Localhost only.
// "Connect Supabase": returns the Supabase approval page, for this project or (before one is set) the whole account. Localhost only.
export async function POST(request: Request) {
const host = request.headers.get("host") ?? "";
if (!LOOPBACK.test(host) || !(await setupAccess()).allowed) return NextResponse.json({ error: "Only on this computer." }, { status: 403 });
// No project yet: sign in to the whole account, and broke picks (or makes) the project after.
const ref = refFromUrl(currentEnv().NEXT_PUBLIC_SUPABASE_URL);
if (!ref) return NextResponse.json({ error: "Add your Supabase Project URL first." }, { status: 400 });
try {
const url = await startConnect(ref, `http://${host}/api/setup/supabase/callback`);
return NextResponse.json(url ? { url } : { connected: true });
@@ -0,0 +1,32 @@
import { NextResponse } from "next/server";
import { sameOrigin, setupAccess } from "@/lib/setup";
import { fillFrom } from "@/lib/supabase-fill";
import { accountConnected, createProject, listProjects } from "@/lib/supabase-mcp";
const LOOPBACK = /^(localhost|127\.0\.0\.1|\[::1\])(:\d+)?$/;
const local = async (request: Request) => LOOPBACK.test(request.headers.get("host") ?? "") && (await setupAccess()).allowed && accountConnected();
// After "Connect Supabase" signed in to the whole account: list its projects, or use one (or a new one). Using one writes
// its address and publishable key to .env.local and creates broke's tables through the same sign-in. Localhost only.
export async function GET(request: Request) {
if (!(await local(request))) return NextResponse.json({ error: "Only on this computer, after Connect Supabase." }, { status: 403 });
try {
return NextResponse.json({ projects: await listProjects() });
} catch (e) {
console.error("[broke] supabase projects:", e instanceof Error ? e.message : e);
return NextResponse.json({ error: "Couldn't list your Supabase projects. Press Connect Supabase again." }, { status: 502 });
}
}
export async function POST(request: Request) {
if (!(await local(request)) || !sameOrigin(request)) return NextResponse.json({ error: "Only on this computer, after Connect Supabase." }, { status: 403 });
const { ref, create } = (await request.json().catch(() => ({}))) as { ref?: string; create?: boolean };
try {
const id = create ? await createProject() : ref && /^[a-z0-9]{20}$/.test(ref) ? ref : null;
if (!id) return NextResponse.json({ error: "Pick a project." }, { status: 400 });
return NextResponse.json(await fillFrom(id));
} catch (e) {
console.error("[broke] supabase project:", e instanceof Error ? e.message : e);
return NextResponse.json({ error: e instanceof Error && /cost money|organization/.test(e.message) ? e.message : "Supabase didn't answer. Try again in a moment." }, { status: 502 });
}
}