generated from lucien/api-template
Added API endpoints and dockerfile
This commit is contained in:
parent
924e55b4f3
commit
5ddad2ed61
31 changed files with 3885 additions and 942 deletions
72
back/api/channels.js
Normal file
72
back/api/channels.js
Normal file
|
@ -0,0 +1,72 @@
|
|||
const express = require('express');
|
||||
const jwt = require('jsonwebtoken');
|
||||
const { getConnection, getUser, getChannels, getChannel, addChannel, getMessages, addMessage } = require('../libs/mysql');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.get('/', async (req, res) => {
|
||||
const connection = await getConnection();
|
||||
const channels = await getChannels(connection);
|
||||
connection.end();
|
||||
res.send(channels);
|
||||
});
|
||||
|
||||
router.get('/:id', async (req, res) => {
|
||||
const id = req.params.id;
|
||||
const connection = await getConnection();
|
||||
const channel = await getChannel(connection, id);
|
||||
connection.end();
|
||||
if (channel[0]) {
|
||||
res.send(channel[0]);
|
||||
} else {
|
||||
res.send('No channel found');
|
||||
}
|
||||
});
|
||||
|
||||
router.get('/:id/messages', async (req, res) => {
|
||||
const id = req.params.id;
|
||||
const connection = await getConnection();
|
||||
const channel = await getChannel(connection, id);
|
||||
if (!channel[0]) {
|
||||
connection.end();
|
||||
return res.send('No channel found');
|
||||
}
|
||||
const messages = await getMessages(connection, id);
|
||||
connection.end();
|
||||
res.send(messages);
|
||||
});
|
||||
|
||||
router.post('/:id/messages/send', async (req, res) => {
|
||||
const { token, message } = req.body;
|
||||
const id = req.params.id;
|
||||
const connection = await getConnection();
|
||||
|
||||
const decoded = jwt.verify(token, process.env.JWT_SECRET);
|
||||
const user = await getUser(connection, decoded.id);
|
||||
if (!user[0]) {
|
||||
connection.end();
|
||||
return res.status(401).send({ error: 'Invalid token' });
|
||||
}
|
||||
|
||||
await addMessage(connection, id, user[0].id, message.replace("\"", "'"));
|
||||
connection.end();
|
||||
res.send({ message: 'Message sent' });
|
||||
});
|
||||
|
||||
router.post('/add', async (req, res) => {
|
||||
const { name, description, token } = req.body;
|
||||
const connection = await getConnection();
|
||||
|
||||
const decoded = jwt.verify(token, process.env.JWT_SECRET);
|
||||
const user = await getUser(connection, decoded.id);
|
||||
if (!user[0]) {
|
||||
connection.end();
|
||||
return res.status(401).send({ error: 'Invalid token' });
|
||||
}
|
||||
|
||||
await addChannel(connection, name, description);
|
||||
connection.end();
|
||||
res.send({ message: 'Channel added' });
|
||||
});
|
||||
|
||||
module.exports = router;
|
24
back/api/connect.js
Normal file
24
back/api/connect.js
Normal file
|
@ -0,0 +1,24 @@
|
|||
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;
|
28
back/api/users.js
Normal file
28
back/api/users.js
Normal file
|
@ -0,0 +1,28 @@
|
|||
const express = require('express');
|
||||
const sha256 = require("sha256");
|
||||
const { getConnection, getUser, addUser } = require('../libs/mysql');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.get('/:id', async (req, res) => {
|
||||
const id = req.params.id;
|
||||
const connection = await getConnection();
|
||||
const users = await getUser(connection, id);
|
||||
connection.end();
|
||||
if (users[0]) {
|
||||
res.send(users[0]);
|
||||
} 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;
|
Loading…
Add table
Add a link
Reference in a new issue