fix: fixed user creation and channel creation

This commit is contained in:
Lukian 2025-03-24 20:38:54 +01:00
parent 42eb432b58
commit 5d13a51f8e
3 changed files with 19 additions and 1 deletions

View file

@ -24,6 +24,13 @@ router.post('/login', async (req, res) => {
router.post('/register', async (req, res) => { router.post('/register', async (req, res) => {
const { username, password } = req.body; const { username, password } = req.body;
const connection = await getConnection(); 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); const hash = sha256(password);
await addUser(connection, username, hash); await addUser(connection, username, hash);
connection.end(); connection.end();

View file

@ -70,6 +70,17 @@ router.post('/add', async (req, res) => {
return res.status(401).send({ error: 'Invalid token' }); 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); await addChannel(connection, name, description);
connection.end(); connection.end();
res.send({ message: 'Channel added' }); res.send({ message: 'Channel added' });

View file

@ -36,6 +36,7 @@ export default function Home() {
localStorage.removeItem("token") localStorage.removeItem("token")
window.location.reload() window.location.reload()
}}>Logout</button> }}>Logout</button>
<Link to="/create-channel">Create Channel</Link>
</div> </div>
) : ( ) : (
<div> <div>
@ -43,7 +44,6 @@ export default function Home() {
<Link to="/register">Register</Link> <Link to="/register">Register</Link>
</div> </div>
)} )}
<Link to="/create-channel">Create Channel</Link>
<h2>Channels</h2> <h2>Channels</h2>
<ul> <ul>
{channels?.map((channel) => ( {channels?.map((channel) => (