still trying to display username and message from database
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user