logigate/server/api/auth/delete.post.ts

16 lines
695 B
TypeScript
Raw Permalink Normal View History

// Delete the account and every project stored with it.
export default defineEventHandler(async event => {
rateLimit(event, 'login', 20, 10 * 60_000)
const user = await requireUser(event)
const body = await readBody<{ password?: unknown }>(event)
if (typeof body?.password !== 'string' || !(await verifyPassword(body.password, user))) {
throw createError({ statusCode: 403, message: 'The password is not right' })
}
await withLock(user.id, () => deleteAccountData(user.id))
await endAllSessions(user.id)
await users().removeItem(emailKey(user.email))
await users().removeItem(`user:${user.id}`)
deleteCookie(event, SESSION_COOKIE, { path: '/' })
return { ok: true }
})