Clear my data on the Database page
Disconnects every bank at Plaid and deletes the signed-in user's money data. Sign-in, profile and AI connection stay. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9c4bf6198e
commit
f2d7f36017
5 files changed
+97
-17
No files matched your search
@@ -2,7 +2,7 @@
|
||||
|
||||
import { Wrench, Bot, Check, CircleAlert, X, Database, Landmark, Link2, Palette, RefreshCw, SlidersHorizontal, User, type LucideIcon } from "lucide-react";
|
||||
import Link from "next/link";
|
||||
import { ClearTestData, RemoveTest } from "@/components/ClearTestData";
|
||||
import { ClearMyData, ClearTestData, RemoveTest } from "@/components/ClearTestData";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
import { modelOptions, saveChoice } from "@/components/Assistant";
|
||||
@@ -65,7 +65,7 @@ export function Settings({ signedIn, local, folder, repo, email, profile, mode,
|
||||
else setChecking(false);
|
||||
const fromHash = () => {
|
||||
const h = location.hash.slice(1) as Group;
|
||||
if (NAV.some((n) => n.id === h) && (local || !SERVER.includes(h) || h === "banks")) setGroup(h);
|
||||
if (NAV.some((n) => n.id === h) && (local || !SERVER.includes(h) || h === "banks" || h === "database")) setGroup(h);
|
||||
};
|
||||
fromHash();
|
||||
addEventListener("hashchange", fromHash);
|
||||
@@ -77,7 +77,7 @@ export function Settings({ signedIn, local, folder, repo, email, profile, mode,
|
||||
if (local && group === "banks") load();
|
||||
}, [group, local, load]);
|
||||
|
||||
const nav = local ? NAV : NAV.filter((n) => !SERVER.includes(n.id) || n.id === "banks");
|
||||
const nav = local ? NAV : NAV.filter((n) => !SERVER.includes(n.id) || n.id === "banks" || n.id === "database");
|
||||
const sections = (data?.sections ?? []).filter((s) => groupOf(s) === group);
|
||||
const flagged = (g: Group) => (data?.sections ?? []).some((s) => groupOf(s) === g && attention(s));
|
||||
const here = NAV.find((n) => n.id === group)!;
|
||||
@@ -125,6 +125,11 @@ export function Settings({ signedIn, local, folder, repo, email, profile, mode,
|
||||
</Card>
|
||||
)}
|
||||
{group === "improve" && <ImproveCard folder={folder} repo={repo} />}
|
||||
{group === "database" && (
|
||||
<Card title="Clear my data" what="Disconnects every bank and deletes your accounts, transactions, bills, budgets, rules, plans and chats. Your sign-in, profile answers and AI connection stay. Only your own data is touched.">
|
||||
<ClearMyData />
|
||||
</Card>
|
||||
)}
|
||||
{group === "ai" && <AiDataCard initial={aiMode} />}
|
||||
{group === "ai" && <ModelCard />}
|
||||
{!local && (SERVER.includes(group) || group === "ai") && (
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { revokeItem } from "@/lib/plaid";
|
||||
import { sameOrigin } from "@/lib/setup";
|
||||
import { createAdminClient } from "@/utils/supabase/admin";
|
||||
import { createClient } from "@/utils/supabase/server";
|
||||
|
||||
// Settings, Database, "Clear my data": the signed-in user's money data, and nobody else's. Bank connections are revoked
|
||||
// at Plaid first, then deleted (accounts, transactions, bills and holdings cascade). Sign-in, profile answers, categories
|
||||
// and the AI connection stay, so they can link again or use a test bank straight away.
|
||||
const TABLES = ["accounts", "transactions", "recurring", "budgets", "rules", "plans", "checkins", "chat_threads", "bookmarks"];
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const { data: { user } } = await (await createClient()).auth.getUser();
|
||||
if (!user) return NextResponse.json({ error: "Not signed in" }, { status: 401 });
|
||||
if (!sameOrigin(request)) return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
||||
const { confirm } = (await request.json().catch(() => ({}))) as { confirm?: string };
|
||||
if (confirm !== "clear") return NextResponse.json({ error: "Not confirmed" }, { status: 400 });
|
||||
|
||||
const db = createAdminClient();
|
||||
const { data: items } = await db.from("plaid_items").select("id, access_token, provider, institution_name").eq("user_id", user.id);
|
||||
const stuck: string[] = [];
|
||||
for (const item of items ?? []) {
|
||||
if (!(await revokeItem(item)).ok) { stuck.push(item.institution_name ?? "A bank"); continue; }
|
||||
await db.from("plaid_items").delete().eq("id", item.id);
|
||||
}
|
||||
// Manual accounts and anything left over. A bank Plaid wouldn't let go of keeps its data, so it can be tried again.
|
||||
if (!stuck.length) for (const t of TABLES) {
|
||||
const { error } = await db.from(t).delete().eq("user_id", user.id);
|
||||
if (error) {
|
||||
console.error(`[broke] clear ${t}:`, error.message);
|
||||
return NextResponse.json({ error: "Couldn't clear everything. Try again." }, { status: 500 });
|
||||
}
|
||||
}
|
||||
if (stuck.length) return NextResponse.json({ error: `Plaid couldn't disconnect ${stuck.join(", ")}. Try again in a moment.` }, { status: 502 });
|
||||
return NextResponse.json({ ok: true });
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { plaid, plaidError } from "@/lib/plaid";
|
||||
import { revokeItem } from "@/lib/plaid";
|
||||
import { createAdminClient } from "@/utils/supabase/admin";
|
||||
import { createClient } from "@/utils/supabase/server";
|
||||
|
||||
@@ -14,19 +14,7 @@ export async function POST(request: Request) {
|
||||
const { data: item } = itemId ? await db.from("plaid_items").select("id, access_token, provider").eq("id", itemId).eq("user_id", user.id).maybeSingle() : { data: null };
|
||||
if (!item) return NextResponse.json({ error: "Connection not found" }, { status: 404 });
|
||||
|
||||
// Akahu personal-app tokens belong to the server owner: removing here only forgets the connection in broke.
|
||||
// A test (sandbox) connection can't be revoked from live mode; Plaid's sandbox forgets it anyway.
|
||||
const testInLive = item.access_token?.startsWith("access-sandbox-") && process.env.PLAID_ENV !== "sandbox";
|
||||
if (item.provider === "plaid" && !testInLive) try {
|
||||
await plaid.itemRemove({ access_token: item.access_token! });
|
||||
} catch (e) {
|
||||
const err = plaidError(e);
|
||||
// Already gone at Plaid: still remove it here.
|
||||
if (err.code !== "ITEM_NOT_FOUND" && err.code !== "INVALID_ACCESS_TOKEN") {
|
||||
console.error("[broke] item remove:", err);
|
||||
return NextResponse.json({ error: "Plaid couldn't remove this connection. Try again in a moment." }, { status: 502 });
|
||||
}
|
||||
}
|
||||
if (!(await revokeItem(item)).ok) return NextResponse.json({ error: "Plaid couldn't remove this connection. Try again in a moment." }, { status: 502 });
|
||||
const { error } = await db.from("plaid_items").delete().eq("id", item.id);
|
||||
if (error) return NextResponse.json({ error: "Couldn't remove this connection." }, { status: 500 });
|
||||
return NextResponse.json({ ok: true });
|
||||
|
||||
Reference in new issue
Block a user