adding chat
This commit is contained in:
24
backend/socket.js
Normal file
24
backend/socket.js
Normal file
@@ -0,0 +1,24 @@
|
||||
const { Server } = require("socket.io");
|
||||
const { createServer } = require('node:http');
|
||||
const { join } = require('node:path');
|
||||
|
||||
const express = require('express');
|
||||
const app = express();
|
||||
const server = createServer(app);
|
||||
const io = new Server(server);
|
||||
|
||||
function chat(req, res) {
|
||||
io.on('connection', (socket) => {
|
||||
console.log('a user connected');
|
||||
|
||||
socket.on('disconnect', () => {
|
||||
console.log('user disconnected');
|
||||
});
|
||||
|
||||
socket.on('connection', (socket) => {
|
||||
console.log('message: ', + msg);
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = chat;
|
||||
13
frontend/js/chat.js
Normal file
13
frontend/js/chat.js
Normal file
@@ -0,0 +1,13 @@
|
||||
const socket = io();
|
||||
|
||||
const form = document.getElementById('form');
|
||||
const input = document.getElementById('input');
|
||||
const cos = document.getElementById('cos');
|
||||
let cos = jklsdfjklfsdjkldfsjkldsfklj
|
||||
form.addEventListener('submit', (e) => {
|
||||
e.preventDefault();
|
||||
if (input.value) {
|
||||
socket.emit('chat message', input.value);
|
||||
input.value = '';
|
||||
}
|
||||
});
|
||||
@@ -11,12 +11,15 @@ function showPasswd() {
|
||||
}
|
||||
|
||||
document.getElementById('signupForm').addEventListener('submit',async function (event) {
|
||||
event.preventDefault();
|
||||
|
||||
const password = document.getElementById("password").value;
|
||||
const sPassword = document.getElementById("sPassword").value;
|
||||
console.log(password, sPassword);
|
||||
|
||||
//TODO it doesnt work lmao
|
||||
if(password !== sPassword){
|
||||
alert("Passwords don't match!");
|
||||
event.preventDefault();
|
||||
} else {
|
||||
this.submit();
|
||||
}
|
||||
})
|
||||
|
||||
@@ -6,8 +6,16 @@
|
||||
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
|
||||
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
||||
<title>Chat</title>
|
||||
<script src="../js/chat.js" defer></script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<ul id="messages"></ul>
|
||||
<form id="form" action="">
|
||||
<input id="input" autocomplete="off" />
|
||||
<button>Send</button>
|
||||
</form>
|
||||
<p id="cos"></p>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
7
index.js
7
index.js
@@ -4,7 +4,8 @@ const port = 3000
|
||||
const app = express()
|
||||
|
||||
const path = require('path');
|
||||
const { insertUser, isUserExists, client } = require('./backend/db');
|
||||
const { insertUser, isUserExists, client } = require('./backend/db.js');
|
||||
const { chat } = require('./backend/socket.js');
|
||||
|
||||
const bcrypt = require('bcrypt');
|
||||
const saltRounds = 10;
|
||||
@@ -97,10 +98,10 @@ async function loginUser(req, res) {
|
||||
if (username && password) {
|
||||
try {
|
||||
const result = await client.query('SELECT * FROM accounts WHERE username = $1', [username]);
|
||||
// check if user exists
|
||||
if (result.rows.length > 0) {
|
||||
const user = result.rows[0];
|
||||
// Compare password
|
||||
const match = await bcrypt.compare(password, user.password);
|
||||
const match = await bcrypt.compare(password, result.rows[0].password);
|
||||
if (match) {
|
||||
req.session.loggedin = true;
|
||||
req.session.username = username;
|
||||
|
||||
Reference in New Issue
Block a user