generated from lucien/api-template
add: added a user page and button to delete them
This commit is contained in:
parent
6c19015a29
commit
4045681913
8 changed files with 235 additions and 1 deletions
|
@ -50,6 +50,12 @@ router.post('/register', async (req, res) => {
|
|||
const hash = sha256(password);
|
||||
await addUser(connection, username, hash);
|
||||
connection.end();
|
||||
|
||||
req.sockets.emit({
|
||||
type: 'new_user',
|
||||
username: username,
|
||||
});
|
||||
|
||||
res.send({ message: 'User added' });
|
||||
});
|
||||
|
||||
|
|
|
@ -1,8 +1,16 @@
|
|||
const express = require('express');
|
||||
const { getConnection, getUserByUsername, getUserLastMessages, getMentions } = require('../libs/mysql');
|
||||
const { getConnection, getUsers, getUserByUsername, getUserLastMessages, getMentions, deleteUser, deleteUserMessages, deleteUserMentions } = require('../libs/mysql');
|
||||
const { checkAuth } = require("../libs/middlewares")
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.get('/', async (req, res) => {
|
||||
const connection = await getConnection();
|
||||
const users = await getUsers(connection);
|
||||
connection.end();
|
||||
res.send(users);
|
||||
});
|
||||
|
||||
router.get('/:username', async (req, res) => {
|
||||
const username = req.params.username;
|
||||
const connection = await getConnection();
|
||||
|
@ -33,4 +41,37 @@ router.get('/:username/lastmessages', async (req, res) => {
|
|||
res.send(messages);
|
||||
});
|
||||
|
||||
router.use("/:username/delete", checkAuth);
|
||||
router.post('/:username/delete', async (req, res) => {
|
||||
const username = req.params.username;
|
||||
const user = req.user;
|
||||
|
||||
const connection = await getConnection();
|
||||
|
||||
const userToDelete = await getUserByUsername(connection, username);
|
||||
|
||||
if (!userToDelete[0]) {
|
||||
connection.end();
|
||||
return res.status(400).send({ error: 'No user found' });
|
||||
}
|
||||
|
||||
if (user.admin !== 1) {
|
||||
connection.end();
|
||||
return res.status(401).send({ error: 'Unauthorized' });
|
||||
}
|
||||
|
||||
await deleteUserMentions(connection, userToDelete[0].id);
|
||||
await deleteUserMessages(connection, userToDelete[0].id);
|
||||
await deleteUser(connection, userToDelete[0].id);
|
||||
|
||||
connection.end();
|
||||
|
||||
req.sockets.emit({
|
||||
type: 'delete_user',
|
||||
user_id: userToDelete[0].id,
|
||||
});
|
||||
|
||||
res.send({ message: 'User deleted' });
|
||||
});
|
||||
|
||||
module.exports = router;
|
Loading…
Add table
Add a link
Reference in a new issue