Skip to content

Commit

Permalink
chore(profile): delete account (#826)
Browse files Browse the repository at this point in the history
  • Loading branch information
stavares843 authored Nov 11, 2024
1 parent fc1e578 commit 76cfd93
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/lib/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,8 @@
"label": "Log Out",
"description": "Log out of the current account and return to the unlock page."
},
"delete_title": "Delete Account",
"delete_subtitle": "Click here to delete your account",
"change_profile_photo": "Change profile photo",
"reveal_phrase": {
"label": "Reveal recovery phrase",
Expand Down
76 changes: 76 additions & 0 deletions src/routes/settings/profile/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,69 @@
isValidUsernameToUpdate = false
}
// Function to delete IndexedDB database by name
function deleteIndexedDB(dbName) {
return new Promise((resolve, reject) => {
const request = indexedDB.deleteDatabase(dbName)
request.onsuccess = function () {
console.log(`Database '${dbName}' deleted successfully.`)
resolve()
}
request.onerror = function () {
console.error(`Failed to delete database '${dbName}':`, request.error)
reject(request.error)
}
request.onblocked = function () {
console.warn(`Database deletion for '${dbName}' is blocked. Close other tabs that use it and try again.`)
// Continue even if blocked, but mark as incomplete
resolve('Blocked')
}
})
}
// Function to clear all IndexedDB data, localStorage, sessionStorage, and cookies
async function clearAllData() {
try {
// Clear localStorage and sessionStorage first
localStorage.clear()
console.log("localStorage cleared.")
sessionStorage.clear()
console.log("sessionStorage cleared.")
// Attempt to delete specific database 'tesseract' and all other IndexedDB databases
await deleteIndexedDB('tesseract')
console.log("Database 'tesseract' cleared if it existed.")
const dbNames = await indexedDB.databases()
for (let dbInfo of dbNames) {
if (dbInfo.name) {
const result = await deleteIndexedDB(dbInfo.name)
if (result === 'Blocked') {
console.warn(`Could not delete database '${dbInfo.name}' due to blocking issues.`)
}
}
}
console.log("All IndexedDB data cleared, where not blocked.")
// Clear cookies
document.cookie.split(';').forEach(cookie => {
const cookieName = cookie.split('=')[0].trim()
document.cookie = cookieName + '=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/'
})
console.log("Cookies cleared.")
// Redirect to '/auth' with cache busting to prevent stale cache loading
window.location.href = '/auth?cacheBust=' + new Date().getTime()
} catch (error) {
console.error("Error clearing data:", error)
}
}
$: auth = AuthStore.state
$: saveSeedPhrase = $auth.saveSeedPhrase
$: showSeed = seedPhrase ? SeedState.Hidden : SeedState.Missing
Expand Down Expand Up @@ -571,6 +634,19 @@
</Button>
</SettingSection>
</div>
<div class="section">
<SettingSection hook="section-delete-account" name={$_("settings.profile.delete_title")} description={$_("settings.profile.delete_subtitle")}>
<Button
hook="button-delete-account"
appearance={Appearance.Alt}
text={$_("settings.profile.delete_title")}
on:click={_ => {
clearAllData()
}}>
<Icon icon={Shape.Lock} />
</Button>
</SettingSection>
</div>
</div>
</div>
</div>
Expand Down

0 comments on commit 76cfd93

Please sign in to comment.