delete your own account endpoint

This commit is contained in:
linkability 2023-10-05 20:08:39 +01:00
parent c51d8dab95
commit 01bb4684a3
No known key found for this signature in database

View file

@ -1,5 +1,6 @@
// Modules
import { writeFile } from 'fs'
import { Router } from "express";
import bodyParser from "body-parser";
@ -142,5 +143,35 @@ module.exports = function(files: Files) {
}
)
router.delete("/me",
requiresAccount,
noAPIAccess,
parser,
(req, res) => {
const Account = res.locals.acc as Accounts.Account
const accountId = Account.id
Authentication.AuthTokens.filter(e => e.account == accountId).forEach((token) => {
Authentication.invalidate(token.token)
})
const deleteAccount = () => Accounts.deleteAccount(accountId).then(_ => res.send("account deleted"))
if (req.body.deleteFiles) {
const Files = Account.files.map(e => e)
for (let fileId of Files) {
files.unlink(fileId, true).catch(err => console.error)
}
writeFile(process.cwd() + "/.data/files.json", JSON.stringify(files.files), (err) => {
if (err) console.log(err)
deleteAccount()
})
} else deleteAccount()
}
)
return router
}