forked from jordanlambrecht/tracker-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute.ts
More file actions
54 lines (45 loc) · 1.84 KB
/
Copy pathroute.ts
File metadata and controls
54 lines (45 loc) · 1.84 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// src/app/api/settings/nuke/route.ts
//
// Functions: POST
import { NextResponse } from "next/server"
import { authenticate, parseJsonBody } from "@/lib/api-helpers"
import { clearSession, verifyPassword } from "@/lib/auth"
import { db } from "@/lib/db"
import { appSettings } from "@/lib/db/schema"
import { errMsg } from "@/lib/error-utils"
import { PASSWORD_MAX } from "@/lib/limits"
import { log } from "@/lib/logger"
import { scrubAndDeleteAll } from "@/lib/nuke"
import { ensureSchedulerRunning } from "@/lib/scheduler"
export async function POST(request: Request) {
const auth = await authenticate()
if (auth instanceof NextResponse) return auth
const body = await parseJsonBody(request)
if (body instanceof NextResponse) return body
const { password } = body as { password?: string }
if (!password || typeof password !== "string" || password.length > PASSWORD_MAX) {
return NextResponse.json({ error: "Master password is required" }, { status: 400 })
}
const [settings] = await db.select().from(appSettings).limit(1)
if (!settings) {
return NextResponse.json({ error: "Not configured" }, { status: 400 })
}
const valid = await verifyPassword(settings.passwordHash, password)
if (!valid) {
log.warn({ route: "POST /api/settings/nuke" }, "data scrub rejected — incorrect password")
return NextResponse.json({ error: "Incorrect password" }, { status: 401 })
}
log.info({ route: "POST /api/settings/nuke" }, "data scrub initiated")
try {
await scrubAndDeleteAll()
} catch (err) {
log.error({ route: "POST /api/settings/nuke", error: errMsg(err) }, "Data scrub failed")
ensureSchedulerRunning(auth.encryptionKey)
return NextResponse.json(
{ error: "Data scrub failed. Your data is unchanged." },
{ status: 500 }
)
}
await clearSession()
return NextResponse.json({ success: true })
}