add: added homepage

This commit is contained in:
Lukian 2025-03-24 14:41:48 +01:00
parent 1f24907c80
commit 4ea8c9f2b3
11 changed files with 239 additions and 68 deletions

46
back/api/auth.js Normal file
View file

@ -0,0 +1,46 @@
const express = require('express');
const sha256 = require("sha256");
const jwt = require('jsonwebtoken');
const { getConnection, getUserByUsername, addUser, getUser } = require('../libs/mysql');
const router = express.Router();
router.post('/login', async (req, res) => {
const { username, password } = req.body;
const connection = await getConnection();
const users = await getUserByUsername(connection, username);
connection.end();
if (users[0]) {
if (users[0].password === sha256(password)) {
const token = jwt.sign({ id: users[0].id }, process.env.JWT_SECRET, {
expiresIn: 1000 * 60 * 60 * 24 * 7,
});
return res.send({ token: token });
}
}
res.status(401).send({ error: 'Invalid username or password' });
});
router.post('/register', async (req, res) => {
const { username, password } = req.body;
const connection = await getConnection();
const hash = sha256(password);
await addUser(connection, username, hash);
connection.end();
res.send({ message: 'User added' });
});
router.post('/me', async (req, res) => {
const { token } = req.body;
const decoded = jwt.verify(token, process.env.JWT_SECRET);
const connection = await getConnection();
const users = await getUser(connection, decoded.id);
connection.end();
if (users[0]) {
res.send({ id: users[0].id, username: users[0].username });
} else {
res.status(401).send({ error: 'Invalid token' });
}
});
module.exports = router;

View file

@ -11,10 +11,10 @@ router.get('/', async (req, res) => {
res.send(channels);
});
router.get('/:id', async (req, res) => {
const id = req.params.id;
router.get('/:name', async (req, res) => {
const name = req.params.name;
const connection = await getConnection();
const channel = await getChannel(connection, id);
const channel = await getChannel(connection, name);
connection.end();
if (channel[0]) {
res.send(channel[0]);
@ -23,22 +23,22 @@ router.get('/:id', async (req, res) => {
}
});
router.get('/:id/messages', async (req, res) => {
const id = req.params.id;
router.get('/:name/messages', async (req, res) => {
const name = req.params.name;
const connection = await getConnection();
const channel = await getChannel(connection, id);
const channel = await getChannel(connection, name);
if (!channel[0]) {
connection.end();
return res.send('No channel found');
}
const messages = await getMessages(connection, id);
const messages = await getMessages(connection, channel[0].id);
connection.end();
res.send(messages);
});
router.post('/:id/messages/send', async (req, res) => {
router.post('/:name/messages/send', async (req, res) => {
const { token, message } = req.body;
const id = req.params.id;
const name = req.params.name;
const connection = await getConnection();
const decoded = jwt.verify(token, process.env.JWT_SECRET);
@ -48,7 +48,13 @@ router.post('/:id/messages/send', async (req, res) => {
return res.status(401).send({ error: 'Invalid token' });
}
await addMessage(connection, id, user[0].id, message.replace("\"", "'"));
const channel = await getChannel(connection, name);
if (!channel[0]) {
connection.end();
return res.send('No channel found');
}
await addMessage(connection, channel[0].id, user[0].id, message.replace("\"", "'"));
connection.end();
res.send({ message: 'Message sent' });
});

View file

@ -1,24 +0,0 @@
const express = require('express');
const sha256 = require("sha256");
const jwt = require('jsonwebtoken');
const { getConnection, getUserByUsername } = require('../libs/mysql');
const router = express.Router();
router.post('/', async (req, res) => {
const { username, password } = req.body;
const connection = await getConnection();
const users = await getUserByUsername(connection, username);
connection.end();
if (users[0]) {
if (users[0].password === sha256(password)) {
const token = jwt.sign({ id: users[0].id }, process.env.JWT_SECRET, {
expiresIn: 1000 * 60 * 60 * 24 * 7,
});
res.send({ token: token });
}
}
res.status(401).send({ error: 'Invalid username or password' });
});
module.exports = router;

View file

@ -10,19 +10,10 @@ router.get('/:id', async (req, res) => {
const users = await getUser(connection, id);
connection.end();
if (users[0]) {
res.send(users[0]);
res.send({id: users[0].id, username: users[0].username});
} else {
res.send('No user found');
}
});
router.post('/add', async (req, res) => {
const { username, password } = req.body;
const connection = await getConnection();
const hash = sha256(password);
await addUser(connection, username, hash);
connection.end();
res.send({ message: 'User added' });
});
module.exports = router;

View file

@ -65,10 +65,10 @@ function getChannels(connection) {
});
}
function getChannel(connection, id) {
function getChannel(connection, name) {
return new Promise((resolve, reject) => {
connection.query(
`SELECT * FROM channels WHERE id = ${id}`,
`SELECT * FROM channels WHERE name = "${name}"`,
(error, result) => {
if (error) {
reject(new Error(error));