diff --git a/back/api/auth.js b/back/api/auth.js index c1a2640..ccec218 100644 --- a/back/api/auth.js +++ b/back/api/auth.js @@ -24,6 +24,13 @@ router.post('/login', async (req, res) => { router.post('/register', async (req, res) => { const { username, password } = req.body; const connection = await getConnection(); + + const use = await getUserByUsername(connection, username); + if (use[0]) { + connection.end(); + return res.status(401).send({ error: 'Username already exists' }); + } + const hash = sha256(password); await addUser(connection, username, hash); connection.end(); diff --git a/back/api/channels.js b/back/api/channels.js index 01f24d0..817af9f 100644 --- a/back/api/channels.js +++ b/back/api/channels.js @@ -70,6 +70,17 @@ router.post('/add', async (req, res) => { return res.status(401).send({ error: 'Invalid token' }); } + const channel = await getChannel(connection, name); + if (channel[0]) { + connection.end(); + return res.status(400).send({ error: 'Channel already exists' }); + } + + if (!/^[a-zA-Z0-9-_]+$/.test(name)) { + connection.end(); + return res.status(400).send({ error: 'Invalid channel name' }); + } + await addChannel(connection, name, description); connection.end(); res.send({ message: 'Channel added' }); diff --git a/front/src/pages/Home.tsx b/front/src/pages/Home.tsx index c807d7a..d5d59bf 100644 --- a/front/src/pages/Home.tsx +++ b/front/src/pages/Home.tsx @@ -36,6 +36,7 @@ export default function Home() { localStorage.removeItem("token") window.location.reload() }}>Logout + Create Channel ) : (