diff --git a/src/app/setup/SetupActions.tsx b/src/app/setup/SetupActions.tsx index a06d6cc..40b2efc 100644 --- a/src/app/setup/SetupActions.tsx +++ b/src/app/setup/SetupActions.tsx @@ -102,6 +102,7 @@ export function KeyForm({ fields, canWrite, writeNote, onSaved }: { fields: Fiel )} + {f.help && !f.set && {f.help}} {f.options ? ( void }) { } // One click: Supabase's own sign-in page, then back to /setup with the tables created. -export function ConnectSupabaseButton({ onDone }: { onDone: () => void }) { +export function ConnectSupabaseButton({ onDone, onFail }: { onDone: () => void; onFail?: () => void }) { const [busy, setBusy] = useState(false); const [msg, setMsg] = useState(""); return ( @@ -185,7 +186,10 @@ export function ConnectSupabaseButton({ onDone }: { onDone: () => void }) { const body = await res.json().catch(() => ({})); if (body.url) return void (window.location.href = body.url); setBusy(false); - if (!res.ok) setMsg(body.error ?? "Couldn't start the Supabase sign-in."); + if (!res.ok) { + setMsg(body.error ?? "Couldn't start the Supabase sign-in."); + onFail?.(); + } onDone(); }} > diff --git a/src/app/setup/SetupChecklist.tsx b/src/app/setup/SetupChecklist.tsx index 9b93dc9..0d17d1d 100644 --- a/src/app/setup/SetupChecklist.tsx +++ b/src/app/setup/SetupChecklist.tsx @@ -194,24 +194,35 @@ function Steps({ steps }: { steps?: Section["steps"] }) { } export function Body({ s, data, signedIn, reload }: { s: Section; data: SetupStatus; signedIn: boolean; reload: () => void }) { + const [fallback, setFallback] = useState(false); + const form = s.fields && ; return ( <> {s.note && s.status !== "done" && s.status !== "error" &&

{s.note}

} {s.note && s.status === "done" && s.kind === "more" &&

{s.note}

} - {s.action === "connect" && } + {s.action === "connect" && setFallback(true)} />} {s.action === "connect" && (s.steps || s.fields) ? ( -
- Or paste a connection string instead +
setFallback(e.currentTarget.open)}> + Button didn't work? Paste a connection string instead
- {s.fields && } + {form} +
+
+ ) : s.status === "done" && s.kind !== "more" && s.fields ? ( + // Connected: the steps and empty boxes are noise now. They stay one click away for changing a key. +
+ Change these keys +
+ + {form}
) : ( <> {s.command && } - {s.fields && } + {form} )} {s.action === "migrate" && } diff --git a/src/lib/envfile.test.ts b/src/lib/envfile.test.ts index 588a1bc..03e229d 100644 --- a/src/lib/envfile.test.ts +++ b/src/lib/envfile.test.ts @@ -48,3 +48,9 @@ assert.equal(validate({ NEXT_PUBLIC_SUPABASE_URL: `https://supabase.com/dashboar assert.equal(validate({ NEXT_PUBLIC_SUPABASE_URL: REF }).clean.NEXT_PUBLIC_SUPABASE_URL, `https://${REF}.supabase.co`); assert.equal(validate({ NEXT_PUBLIC_SUPABASE_URL: `https://${REF}.supabase.co/` }).clean.NEXT_PUBLIC_SUPABASE_URL, `https://${REF}.supabase.co`); assert.equal(validate({ NEXT_PUBLIC_SUPABASE_URL: "http://127.0.0.1:54321" }).clean.NEXT_PUBLIC_SUPABASE_URL, "http://127.0.0.1:54321"); + +// The database password goes into the connection string, URL-encoded, and is never saved on its own. +const db = validate({ SUPABASE_DB_URL: "postgresql://postgres.abc:[YOUR-PASSWORD]@aws-0-us-east-1.pooler.supabase.com:5432/postgres", SUPABASE_DB_PASSWORD: "p@ss w/rd" }); +assert.equal(db.clean.SUPABASE_DB_URL, "postgresql://postgres.abc:p%40ss%20w%2Frd@aws-0-us-east-1.pooler.supabase.com:5432/postgres"); +assert.equal("SUPABASE_DB_PASSWORD" in db.clean, false); +assert.ok(validate({ SUPABASE_DB_URL: "postgresql://postgres.abc:[YOUR-PASSWORD]@h.pooler.supabase.com:5432/postgres" }).errors.SUPABASE_DB_URL); diff --git a/src/lib/envfile.ts b/src/lib/envfile.ts index 59f2d66..7be6a9f 100644 --- a/src/lib/envfile.ts +++ b/src/lib/envfile.ts @@ -29,8 +29,10 @@ export const ENV_KEYS: Record = { label: "Database connection string", secret: true, check: (v) => !/^postgres(ql)?:\/\/[^\s]+@[^\s]+$/.test(v) ? "Starts with postgresql:// (copy the Session pooler URI)" - : /\[YOUR-PASSWORD\]/i.test(v) ? "Replace [YOUR-PASSWORD] with your database password" : null, + : /\[YOUR-PASSWORD\]/i.test(v) ? "Type your database password in the box below" : null, }, + // Never saved: validate() puts it into SUPABASE_DB_URL's [YOUR-PASSWORD], so nobody edits the string by hand. + SUPABASE_DB_PASSWORD: { label: "Database password", secret: true, check: () => null }, PLAID_CLIENT_ID: { label: "Client ID", secret: false, check: (v) => (/^[a-f0-9]{24}$/.test(v) ? null : "24 letters and numbers, from Plaid → Developers → Keys") }, PLAID_SECRET: { label: "Secret", secret: true, check: (v) => (/^[a-f0-9]{30}$/.test(v) ? null : "30 letters and numbers, from Plaid → Developers → Keys") }, PLAID_ENV: { label: "Mode", secret: false, check: (v) => (/^(sandbox|production)$/.test(v) ? null : "sandbox or production") }, @@ -86,7 +88,10 @@ export function supabaseUrl(v: string) { return ref ? `https://${ref}.supabase.co` : v.replace(/\/+$/, ""); } -export function validate(updates: Record): { clean: Record; errors: Record } { +export function validate(input: Record): { clean: Record; errors: Record } { + const { SUPABASE_DB_PASSWORD: pw, ...updates } = input; + if (typeof pw === "string" && pw && typeof updates.SUPABASE_DB_URL === "string") + updates.SUPABASE_DB_URL = updates.SUPABASE_DB_URL.replace(/\[YOUR-PASSWORD\]/gi, encodeURIComponent(pw)); const clean: Record = {}; const errors: Record = {}; for (const [k, raw] of Object.entries(updates)) { diff --git a/src/lib/setup.ts b/src/lib/setup.ts index e60b61b..dcabfcf 100644 --- a/src/lib/setup.ts +++ b/src/lib/setup.ts @@ -12,7 +12,7 @@ import { akahuGet } from "@/lib/providers/akahu"; export type StepStatus = "done" | "todo" | "error" | "optional"; export type Part = string | { href: string; label: string }; -export type Field = { key: string; label: string; secret: boolean; set: boolean; hint: string | null; placeholder?: string; options?: string[] }; +export type Field = { key: string; label: string; secret: boolean; set: boolean; hint: string | null; placeholder?: string; options?: string[]; help?: string }; export type HowTo = { text: string; href?: string; command?: string }; export type AiRow = { id: string; label: string; ready: boolean; reason?: string; howTo?: HowTo[] }; export type Section = { @@ -101,15 +101,14 @@ export async function setupStatus(signedIn: boolean): Promise { status: sb?.ok ? "done" : sb ? "error" : "todo", note: sb?.ok ? (ref ? `Project ${ref}` : sb.message) : sb?.message, steps: [ - ["Make a free account and project at ", { href: "https://supabase.com/dashboard/new", label: "supabase.com" }, ". Any name works. Save the database password it gives you."], - ["Open ", { href: dash("/settings/api-keys"), label: "Project Settings → API Keys" }, " and copy the publishable and secret keys."], - ["For the Project URL, copy the address from your browser's address bar on that page. broke works out the rest."], - ["Paste all three below and press Save."], + ["Make a free account and a new project at ", { href: "https://supabase.com/dashboard/new", label: "supabase.com" }, ". Any name works. Write down the database password you choose: you need it in the next step if the button there doesn't work."], + ["When the project is ready, open ", { href: dash("/settings/api-keys"), label: "Project Settings → API Keys" }, "."], + ["Copy three things from that page into the boxes below, then press Save."], ], fields: fields(env, ["NEXT_PUBLIC_SUPABASE_URL", "NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY", "SUPABASE_SERVICE_ROLE_KEY"], { - NEXT_PUBLIC_SUPABASE_URL: { placeholder: "https://supabase.com/dashboard/project/… or https://xxxx.supabase.co" }, - NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY: { placeholder: "sb_publishable_…" }, - SUPABASE_SERVICE_ROLE_KEY: { placeholder: "sb_secret_…" }, + NEXT_PUBLIC_SUPABASE_URL: { placeholder: "https://supabase.com/dashboard/project/…", help: "The address in your browser's address bar on the API Keys page." }, + NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY: { placeholder: "sb_publishable_…", help: "Under Publishable key. Starts with sb_publishable_." }, + SUPABASE_SERVICE_ROLE_KEY: { placeholder: "sb_secret_…", help: "Under Secret keys: press the eye to show it, then copy. Starts with sb_secret_." }, }), }; @@ -121,10 +120,14 @@ export async function setupStatus(signedIn: boolean): Promise { note: "Press the button, sign in to Supabase and approve broke. Nothing to copy.", action: "connect", steps: [ - ["In Supabase, click ", { href: dash("?showConnect=true"), label: "Connect" }, " and copy the Session pooler URI."], - ["Put your database password where it says [YOUR-PASSWORD]. Lost it? ", { href: dash("/database/settings"), label: "Reset it here" }, "."], + ["In Supabase, press ", { href: dash("?showConnect=true"), label: "Connect" }, " at the top of your project."], + ["Under Method, choose Session pooler. Copy the connection string."], + ["Paste it below with your database password, then press Save. broke puts the password in for you and creates all its tables at once."], ], - fields: fields(env, ["SUPABASE_DB_URL"], { SUPABASE_DB_URL: { placeholder: "postgresql://postgres.xxxx:password@…pooler.supabase.com:5432/postgres" } }), + fields: fields(env, ["SUPABASE_DB_URL", "SUPABASE_DB_PASSWORD"], { + SUPABASE_DB_URL: { placeholder: "postgresql://postgres.xxxx:[YOUR-PASSWORD]@…pooler.supabase.com:5432/postgres" }, + SUPABASE_DB_PASSWORD: { help: "The one you chose when you made the project. Forgot it? Reset it under Project Settings, Database." }, + }), }; if (!sb?.ok) database = { ...database, note: "Finish the account database first.", action: undefined }; else if (isSet(env.SUPABASE_DB_URL) || mcpConnected(ref)) {