generated from lucien/api-template
add: added user pages, and last messages to the home page
This commit is contained in:
parent
d7af341ca2
commit
87e7c44a5b
10 changed files with 155 additions and 31 deletions
|
@ -31,6 +31,11 @@ router.post('/register', async (req, res) => {
|
|||
return res.status(401).send({ error: 'Username already exists' });
|
||||
}
|
||||
|
||||
if (!/^[a-zA-Z0-9-_]+$/.test(username)) {
|
||||
connection.end();
|
||||
return res.status(400).send({ error: 'Invalid username' });
|
||||
}
|
||||
|
||||
const hash = sha256(password);
|
||||
await addUser(connection, username, hash);
|
||||
connection.end();
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
const express = require('express');
|
||||
const jwt = require('jsonwebtoken');
|
||||
const { getConnection, getUser, getChannels, getChannel, addChannel, getMessages, addMessage, deleteMessage } = require('../libs/mysql');
|
||||
const { getConnection, getUser, getChannels, getChannel, addChannel, getMessages, addMessage, deleteMessage, getLastMessages } = require('../libs/mysql');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
|
|
14
back/api/lastmessages.js
Normal file
14
back/api/lastmessages.js
Normal file
|
@ -0,0 +1,14 @@
|
|||
const express = require('express');
|
||||
const jwt = require('jsonwebtoken');
|
||||
const { getConnection, getLastMessages } = require('../libs/mysql');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.get('/', async (req, res) => {
|
||||
const connection = await getConnection();
|
||||
const messages = await getLastMessages(connection);
|
||||
connection.end();
|
||||
res.send(messages);
|
||||
});
|
||||
|
||||
module.exports = router;
|
|
@ -1,19 +1,26 @@
|
|||
const express = require('express');
|
||||
const sha256 = require("sha256");
|
||||
const { getConnection, getUser, addUser } = require('../libs/mysql');
|
||||
const { getConnection, getUserByUsername, getUserLastMessages } = require('../libs/mysql');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.get('/:id', async (req, res) => {
|
||||
const id = req.params.id;
|
||||
router.get('/:username', async (req, res) => {
|
||||
const username = req.params.username;
|
||||
const connection = await getConnection();
|
||||
const users = await getUser(connection, id);
|
||||
const user = await getUserByUsername(connection, username);
|
||||
connection.end();
|
||||
if (users[0]) {
|
||||
res.send({id: users[0].id, username: users[0].username, admin: users[0].admin});
|
||||
if (user[0]) {
|
||||
res.send({id: user[0].id, username: user[0].username, admin: user[0].admin});
|
||||
} else {
|
||||
res.send('No user found');
|
||||
}
|
||||
});
|
||||
|
||||
router.get('/:username/lastmessages', async (req, res) => {
|
||||
const username = req.params.username;
|
||||
const connection = await getConnection();
|
||||
const messages = await getUserLastMessages(connection, username);
|
||||
connection.end();
|
||||
res.send(messages);
|
||||
});
|
||||
|
||||
module.exports = router;
|
Loading…
Add table
Add a link
Reference in a new issue