🏗️」 wip(auth): added the hability to remove user

This commit is contained in:
2025-10-16 12:29:16 +02:00
parent d80beff543
commit f689274716
4 changed files with 88 additions and 0 deletions

View File

@ -11,6 +11,7 @@ import { totpSetup } from './totpSetup.js';
import { totpDelete } from './totpDelete.js';
import { totpVerify } from './totpVerify.js';
import { logout } from './logout.js';
import { remove } from './remove.js';
const saltRounds = 10;
export const appName = process.env.APP_NAME || 'knl_meowscendence';
@ -110,4 +111,6 @@ export default async function(fastify, options) {
}, async (request, reply) => { return register(request, reply, saltRounds, fastify); });
fastify.get('/logout', {}, async (request, reply) => { return logout(reply, fastify); })
fastify.delete('/', { preHandler: fastify.authenticate }, async (request, reply) => { return remove(request, reply, fastify)})
}

34
src/api/auth/remove.js Normal file
View File

@ -0,0 +1,34 @@
import authDB from '../../utils/authDB';
import { authUserRemove } from '../../utils/authUserRemove';
/**
* @param {import('fastify').FastifyRequest} request
* @param {import('fastify').FastifyReply} reply
* @param {import('fastify').FastifyInstance} fastify
*/
export async function remove(request, reply, fastify) {
try {
const user = request.user;
if (authDB.RESERVED_USERNAMES.includes(user)) {
return reply.code(400).send({ error: 'Reserved username' });
}
if (authDB.checkUser(user) === false) {
return reply.code(400).send({ error: "User does not exist" });
}
authDB.rmUser(user)
authUserRemove(user, fastify)
return reply
.code(200)
.send({
msg: "User successfully deleted"
})
} catch (err) {
fastify.log.error(err);
return reply.code(500).send({ error: "Internal server error" });
}
}