Implement cascading logbook deletion on account deletion

This commit is contained in:
2026-05-28 21:54:30 +02:00
parent ecdf8c2dc0
commit 71ea02416f
6 changed files with 156 additions and 8 deletions
+37
View File
@@ -10,6 +10,7 @@ import {
bufferToBase64
} from './crypto.js'
import { clearLogbookKeysCache } from './logbookKeys.js'
import { db } from './db.js'
const API_BASE = '/api/auth'
@@ -270,3 +271,39 @@ export function logoutUser() {
localStorage.removeItem('active_username')
localStorage.removeItem('active_userid')
}
export async function deleteAccount(): Promise<boolean> {
const userId = localStorage.getItem('active_userid')
if (!userId) return false
try {
const res = await fetch(`${API_BASE}/delete-account`, {
method: 'DELETE',
headers: {
'X-User-Id': userId
}
})
if (res.ok) {
// Clear IndexedDB completely to prevent leaking residual encrypted E2E data on client
await Promise.all([
db.logbooks.clear(),
db.yachts.clear(),
db.crews.clear(),
db.deviations.clear(),
db.entries.clear(),
db.photos.clear(),
db.gpsTracks.clear(),
db.syncQueue.clear(),
db.logbookKeys.clear()
])
// Wipe localStorage and session variables
logoutUser()
return true
}
} catch (err) {
console.error('Failed to delete account:', err)
}
return false
}
+22 -5
View File
@@ -77,6 +77,14 @@ export async function fetchLogbooks(): Promise<DecryptedLogbook[]> {
}
}
}
// Clear local cache for any logbooks that are no longer on the server
const serverIds = new Set(serverLogbooks.map((lb: any) => lb.id))
const localLogbooksArray = await db.logbooks.toArray()
for (const lb of localLogbooksArray) {
if (lb.isSynced === 1 && !serverIds.has(lb.id)) {
await deleteLocalLogbookCache(lb.id)
}
}
// Update Dexie database cache
const localLogbooks: LocalLogbook[] = serverLogbooks.map((lb: any) => ({
@@ -219,6 +227,19 @@ function localIdForCreate(): string {
return tempUUID
}
// Perform cascading deletion on all local Dexie tables for a specific logbook ID
export async function deleteLocalLogbookCache(id: string): Promise<void> {
await db.logbooks.delete(id)
await db.yachts.where({ logbookId: id }).delete()
await db.crews.where({ logbookId: id }).delete()
await db.deviations.where({ logbookId: id }).delete()
await db.entries.where({ logbookId: id }).delete()
await db.photos.where({ logbookId: id }).delete()
await db.gpsTracks.where({ logbookId: id }).delete()
await db.syncQueue.where({ logbookId: id }).delete()
await db.logbookKeys.where({ logbookId: id }).delete()
}
// Delete a logbook and all associated payloads locally and on server
export async function deleteLogbook(id: string): Promise<void> {
const userId = localStorage.getItem('active_userid')
@@ -260,9 +281,5 @@ export async function deleteLogbook(id: string): Promise<void> {
}
// Perform local cascading cleanup
await db.logbooks.delete(id)
await db.yachts.where({ logbookId: id }).delete()
await db.crews.where({ logbookId: id }).delete()
await db.deviations.where({ logbookId: id }).delete()
await db.entries.where({ logbookId: id }).delete()
await deleteLocalLogbookCache(id)
}