From 6ab53f72984681a1d18511ed7b8d996410c93a3c Mon Sep 17 00:00:00 2001 From: Camroku Date: Sun, 19 Jun 2022 17:56:49 +0300 Subject: [PATCH] Better login system + view links as a real link --- index.js | 26 ++++++++-- package-lock.json | 94 ++++++++++++++++++++++++++++++++++ package.json | 1 + static/script/enter.js | 9 ---- static/script/script.js | 1 + views/{index.ejs => login.ejs} | 4 +- 6 files changed, 121 insertions(+), 14 deletions(-) delete mode 100644 static/script/enter.js rename views/{index.ejs => login.ejs} (83%) diff --git a/index.js b/index.js index 5b7d294..d6584bc 100644 --- a/index.js +++ b/index.js @@ -4,14 +4,34 @@ const http = require('http'); const server = http.createServer(app); const { Server } = require("socket.io"); const io = new Server(server); +const session = require('express-session'); + app.set('view engine', 'ejs'); +app.use(express.urlencoded({ extended: false })); +app.use(session({ + secret: 'put-a-really-secret-key-here', + resave: false, + saveUninitialized: true +})); app.get('/', (req, res) => { - res.render('index'); + res.redirect('/login'); +}); + +app.get('/login', (req, res) => { + res.render('login'); +}); + +app.post('/login', (req, res) => { + req.session.username = req.body.username; + res.redirect('/chat'); }); -app.get('/chat/:username', (req, res) => { - res.render('chat', { username: req.params.username }); +app.get('/chat', (req, res) => { + if (req.session.username == null) { + res.redirect('/login'); + } + res.render('chat', { username: req.session.username }); }); app.use(express.static('static')) diff --git a/package-lock.json b/package-lock.json index 25219c0..3d91c54 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,7 @@ "dependencies": { "ejs": "^3.1.8", "express": "^4.18.1", + "express-session": "^1.17.3", "mongodb": "^4.7.0", "socket.io": "^4.5.1" } @@ -458,6 +459,32 @@ "node": ">= 0.10.0" } }, + "node_modules/express-session": { + "version": "1.17.3", + "resolved": "https://registry.npmjs.org/express-session/-/express-session-1.17.3.tgz", + "integrity": "sha512-4+otWXlShYlG1Ma+2Jnn+xgKUZTMJ5QD3YvfilX3AcocOAbIkVylSWEklzALe/+Pu4qV6TYBj5GwOBFfdKqLBw==", + "dependencies": { + "cookie": "0.4.2", + "cookie-signature": "1.0.6", + "debug": "2.6.9", + "depd": "~2.0.0", + "on-headers": "~1.0.2", + "parseurl": "~1.3.3", + "safe-buffer": "5.2.1", + "uid-safe": "~2.1.5" + }, + "engines": { + "node": ">= 0.8.0" + } + }, + "node_modules/express-session/node_modules/cookie": { + "version": "0.4.2", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.4.2.tgz", + "integrity": "sha512-aSWTXFzaKWkvHO1Ny/s+ePFpvKsPnjc551iI41v3ny/ow6tBG5Vd+FuqGNhh1LxOmVzOlGUriIlOaokOvhaStA==", + "engines": { + "node": ">= 0.6" + } + }, "node_modules/filelist": { "version": "1.0.4", "resolved": "https://registry.npmjs.org/filelist/-/filelist-1.0.4.tgz", @@ -780,6 +807,14 @@ "node": ">= 0.8" } }, + "node_modules/on-headers": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/on-headers/-/on-headers-1.0.2.tgz", + "integrity": "sha512-pZAE+FJLoyITytdqK0U5s+FIpjN0JP3OzFi/u8Rx+EV5/W+JTWGXG8xFzevE7AjBfDqHv/8vL8qQsIhHnqRkrA==", + "engines": { + "node": ">= 0.8" + } + }, "node_modules/parseurl": { "version": "1.3.3", "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz", @@ -827,6 +862,14 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/random-bytes": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/random-bytes/-/random-bytes-1.0.0.tgz", + "integrity": "sha512-iv7LhNVO047HzYR3InF6pUcUsPQiHTM1Qal51DcGSuZFBil1aBBWG5eHPNek7bvILMaYJ/8RU1e8w1AMdHmLQQ==", + "engines": { + "node": ">= 0.8" + } + }, "node_modules/range-parser": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz", @@ -1102,6 +1145,17 @@ "node": ">= 0.6" } }, + "node_modules/uid-safe": { + "version": "2.1.5", + "resolved": "https://registry.npmjs.org/uid-safe/-/uid-safe-2.1.5.tgz", + "integrity": "sha512-KPHm4VL5dDXKz01UuEd88Df+KzynaohSL9fBh096KWAxSKZQDI2uBrVqtvRM4rwrIrRRKsdLNML/lnaaVSRioA==", + "dependencies": { + "random-bytes": "~1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, "node_modules/unpipe": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", @@ -1498,6 +1552,28 @@ "vary": "~1.1.2" } }, + "express-session": { + "version": "1.17.3", + "resolved": "https://registry.npmjs.org/express-session/-/express-session-1.17.3.tgz", + "integrity": "sha512-4+otWXlShYlG1Ma+2Jnn+xgKUZTMJ5QD3YvfilX3AcocOAbIkVylSWEklzALe/+Pu4qV6TYBj5GwOBFfdKqLBw==", + "requires": { + "cookie": "0.4.2", + "cookie-signature": "1.0.6", + "debug": "2.6.9", + "depd": "~2.0.0", + "on-headers": "~1.0.2", + "parseurl": "~1.3.3", + "safe-buffer": "5.2.1", + "uid-safe": "~2.1.5" + }, + "dependencies": { + "cookie": { + "version": "0.4.2", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.4.2.tgz", + "integrity": "sha512-aSWTXFzaKWkvHO1Ny/s+ePFpvKsPnjc551iI41v3ny/ow6tBG5Vd+FuqGNhh1LxOmVzOlGUriIlOaokOvhaStA==" + } + } + }, "filelist": { "version": "1.0.4", "resolved": "https://registry.npmjs.org/filelist/-/filelist-1.0.4.tgz", @@ -1728,6 +1804,11 @@ "ee-first": "1.1.1" } }, + "on-headers": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/on-headers/-/on-headers-1.0.2.tgz", + "integrity": "sha512-pZAE+FJLoyITytdqK0U5s+FIpjN0JP3OzFi/u8Rx+EV5/W+JTWGXG8xFzevE7AjBfDqHv/8vL8qQsIhHnqRkrA==" + }, "parseurl": { "version": "1.3.3", "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz", @@ -1760,6 +1841,11 @@ "side-channel": "^1.0.4" } }, + "random-bytes": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/random-bytes/-/random-bytes-1.0.0.tgz", + "integrity": "sha512-iv7LhNVO047HzYR3InF6pUcUsPQiHTM1Qal51DcGSuZFBil1aBBWG5eHPNek7bvILMaYJ/8RU1e8w1AMdHmLQQ==" + }, "range-parser": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz", @@ -1964,6 +2050,14 @@ "mime-types": "~2.1.24" } }, + "uid-safe": { + "version": "2.1.5", + "resolved": "https://registry.npmjs.org/uid-safe/-/uid-safe-2.1.5.tgz", + "integrity": "sha512-KPHm4VL5dDXKz01UuEd88Df+KzynaohSL9fBh096KWAxSKZQDI2uBrVqtvRM4rwrIrRRKsdLNML/lnaaVSRioA==", + "requires": { + "random-bytes": "~1.0.0" + } + }, "unpipe": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", diff --git a/package.json b/package.json index ef1ba58..f85a774 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "dependencies": { "ejs": "^3.1.8", "express": "^4.18.1", + "express-session": "^1.17.3", "mongodb": "^4.7.0", "socket.io": "^4.5.1" } diff --git a/static/script/enter.js b/static/script/enter.js deleted file mode 100644 index 37552bf..0000000 --- a/static/script/enter.js +++ /dev/null @@ -1,9 +0,0 @@ -var form = document.getElementById('form'); -var input = document.getElementById('input'); - -form.addEventListener('submit', function (e) { - e.preventDefault(); - if (input.value) { - window.location.pathname = "/chat/" + input.value; - } -}); \ No newline at end of file diff --git a/static/script/script.js b/static/script/script.js index 9200292..fcf3685 100644 --- a/static/script/script.js +++ b/static/script/script.js @@ -13,6 +13,7 @@ function gotMessage(msg, id) { username.textContent = id; username.className = "username"; message.textContent = msg; + message.innerHTML = message.innerHTML.replace(/(\b(https?|ftp|file):\/\/([-A-Z0-9+&@#%?=~_|!:,.;]*)[-A-Z0-9+&@#%?\/=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig, "$1"); message.className = "message"; row.appendChild(username); row.appendChild(message); diff --git a/views/index.ejs b/views/login.ejs similarity index 83% rename from views/index.ejs rename to views/login.ejs index 8aa96cb..c2fb51d 100644 --- a/views/index.ejs +++ b/views/login.ejs @@ -20,8 +20,8 @@ -
- + +