aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/(app)/settings/delete/+page.server.ts
blob: 05f4f0262146b23d4224ef74693b8aade1a4056f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import { auth, post } from "$lib/utils";
import { fail, redirect } from "@sveltejs/kit";

export async function load({ parent, url }) {
	const { user } = await parent();
	if (!user) redirect(307, `/login?redirect=${encodeURIComponent(url.pathname)}`);
}

export const actions = {
	default: async ({ cookies, request }) => {
		const form = await request.formData();
		const confirm = String(form.get("confirm") || "");
		const pin = String(form.get("pin") || "");

		try {
			// The server wipes everything (lib/deluser), including disabling the
			// account's bolt12 offers, and refuses while a real balance remains.
			await post("/delete", { confirm, ...(pin ? { pin } : {}) }, auth(cookies));
		} catch (e) {
			return fail(400, { error: e?.message || String(e), confirm });
		}

		// The account is gone; /logout clears the session cookies.
		redirect(303, "/logout");
	},
};