#75 remove account

This commit is contained in:
Andrea Vos 2020-10-27 17:33:45 +01:00
parent b85ff19e87
commit 3a05202178
3 changed files with 33 additions and 4 deletions

View File

@ -49,12 +49,17 @@
</ul> </ul>
</template></Loading> </template></Loading>
<p> <section>
<button class="btn btn-outline-secondary btn-sm" @click="logout"> <a href="#" class="badge badge-light border" @click.prevent="logout">
<Icon v="sign-out"/> <Icon v="sign-out"/>
<T>user.logout</T> <T>user.logout</T>
</button> </a>
</p>
<a href="#" class="badge badge-light border" @click.prevent="deleteAccount">
<Icon v="trash-alt"/>
<T>user.deleteAccount</T>
</a>
</section>
</section> </section>
</template> </template>
@ -99,6 +104,13 @@
setProfiles(profiles) { setProfiles(profiles) {
this.profiles = profiles; this.profiles = profiles;
}, },
async deleteAccount() {
await this.$confirm(this.$t('user.deleteAccountConfirm'), 'danger');
const response = await this.$axios.$post(`/user/delete`, {}, { headers: this.$auth() });
this.logout();
},
}, },
} }
</script> </script>

View File

@ -681,6 +681,8 @@ user:
avatar: avatar:
header: 'Avatar' header: 'Avatar'
change: 'Zmień' change: 'Zmień'
deleteAccount: 'Usuń konto'
deleteAccountConfirm: 'Czy na pewno chcesz usunąć swoje konto? Ta operacja jest nieodwracalna!'
profile: profile:
description: 'Opis' description: 'Opis'

View File

@ -161,6 +161,17 @@ const changeUsername = async (db, user, username) => {
return await issueAuthentication(db, user); return await issueAuthentication(db, user);
} }
const removeAccount = async (db, user) => {
const userId = (await db.get(SQL`SELECT id FROM users WHERE username = ${user.username}`)).id;
if (!userId) {
return false;
}
await db.get(SQL`DELETE FROM profiles WHERE userId = ${userId}`)
await db.get(SQL`DELETE FROM authenticators WHERE userId = ${userId}`)
await db.get(SQL`DELETE FROM users WHERE id = ${userId}`)
return true;
}
export default async function (req, res, next) { export default async function (req, res, next) {
const db = await dbConnection(); const db = await dbConnection();
const user = authenticate(req); const user = authenticate(req);
@ -177,5 +188,9 @@ export default async function (req, res, next) {
return renderJson(res, await changeUsername(db, user, req.body.username)); return renderJson(res, await changeUsername(db, user, req.body.username));
} }
if (req.method === 'POST' && req.url === '/delete' && user && user.authenticated) {
return renderJson(res, await removeAccount(db, user));
}
return renderJson(res, {error: 'Not found'}, 404); return renderJson(res, {error: 'Not found'}, 404);
} }