still trying to display username and message from database

This commit is contained in:
slawk0
2024-08-24 13:50:43 +02:00
parent 2b1f0d216f
commit ed934df684

View File

@@ -18,6 +18,7 @@ function initializeSocket(server) {
jwt.verify(token, jwtSecret, (err, user) => {
if(err) {
console.log(err);
return next(new Error('Authentication error'));
}
socket.user = user;
next();
@@ -28,6 +29,11 @@ function initializeSocket(server) {
});
io.on('connection', (socket) => {
if (!socket.user) {
console.log('User not authenticated');
socket.disconnect();
return
}
const username = socket.user.username;
console.log(username + ' connected');
@@ -35,8 +41,9 @@ function initializeSocket(server) {
socket.on('chat message', async (msg) => {
let result;
try {
const result = await db.query('INSERT INTO messages (content) VALUES ($1) RETURNING id', [msg]);
const result = await db.query('INSERT INTO messages (content, username) VALUES ($1, $2) RETURNING id', [msg, username]);
const insertedId = result.rows[0].id;
console.log(result.rows[0])
} catch (err) {
console.error('Error inserting message:', err);
return;