From 5cfb29131c7a71e4db647077b7189022b8f4e118 Mon Sep 17 00:00:00 2001 From: Sam Hadow Date: Wed, 12 Feb 2025 18:44:30 +0100 Subject: [PATCH] pug different view if logged in or out --- src/controllers/account.js | 68 +++++++++++++++++++------------------- src/controllers/main.js | 13 ++++---- src/public/chat.js | 11 ++++++ src/public/popups.js | 8 ++--- src/public/script.js | 12 ------- src/routes/root.js | 4 +++ src/views/index.pug | 13 +++++--- 7 files changed, 69 insertions(+), 60 deletions(-) create mode 100644 src/public/chat.js diff --git a/src/controllers/account.js b/src/controllers/account.js index e90c192..30a9468 100644 --- a/src/controllers/account.js +++ b/src/controllers/account.js @@ -3,42 +3,42 @@ const database = require("../db"); const authentication = require("../authentication"); const accountController = { - getCookie: (req, res) => { - console.log("site loaded") - console.log(req.cookies) - let cookie = req.cookies.user; - if (!cookie) { - //crypto.randomBytes() instead of Math.random() for cryptographically secure random numbers - let randomBuffer = crypto.randomBytes(16); // 128bits of entropy - let randomNumber = randomBuffer.toString('hex'); - let options = { - maxAge: 86400000, // 1 day - httpOnly: true + getCookie: (req, res) => { + console.log("site loaded") + console.log(req.cookies) + let cookie = req.cookies.user; + if (!cookie) { + //crypto.randomBytes() instead of Math.random() for cryptographically secure random numbers + let randomBuffer = crypto.randomBytes(16); // 128bits of entropy + let randomNumber = randomBuffer.toString('hex'); + let options = { + maxAge: 86400000, // 1 day + httpOnly: true + } + // Set cookie + res.cookie("user", randomNumber, options); + console.log("cookie set"); } - // Set cookie - res.cookie("user", randomNumber, options); - console.log("cookie set"); - } - res.redirect('/'); - }, - register: async (req, res) => { - try { - const { sharedSecret, publicKey } = req.body; - if (!sharedSecret || !publicKey) { - return res.status(400).json({ error: "Missing sharedSecret or publicKey" }); + res.redirect('/'); + }, + register: async (req, res) => { + try { + const { sharedSecret, publicKey } = req.body; + if (!sharedSecret || !publicKey) { + return res.status(400).json({ error: "Missing sharedSecret or publicKey" }); + } + console.log('Received data:', { sharedSecret, publicKey }); + if (authentication.checkSharedSecret(sharedSecret)) { + database.addUser(publicKey); + } else { + return res.status(400).json({ error: "Wrong sharedSecret" }); + } + return res.status(201).json({ message: "Registration successful" }); + } catch (error) { + console.error("Error during registration:", error); + return res.status(500).json({ error: "Server error during registration" }); } - console.log('Received data:', { sharedSecret, publicKey }); - if (authentication.checkSharedSecret(sharedSecret)) { - database.addUser(publicKey); - } else { - return res.status(400).json({ error: "Wrong sharedSecret" }); - } - return res.status(201).json({ message: "Registration successful" }); - } catch (error) { - console.error("Error during registration:", error); - return res.status(500).json({ error: "Server error during registration" }); - } - }, + }, loginGetChallenge: async (req, res) => { let randomBuffer = crypto.randomBytes(16); let randomNumber = randomBuffer.toString('hex'); diff --git a/src/controllers/main.js b/src/controllers/main.js index d34d715..6fd216e 100644 --- a/src/controllers/main.js +++ b/src/controllers/main.js @@ -2,12 +2,10 @@ const path = require('path'); const mainController = { root: (req, res) => { - if (typeof req.session.publicKey === 'undefined') { - // main page when not logged in - res.render('index'); - } else { - res.render('index'); - } + let pubKey = req.session.publicKey; + console.log(pubKey); + let isLoggedIn = typeof pubKey !== 'undefined'; + res.render('index', {isLoggedIn, pubKey}); }, style: (req, res) => { res.sendFile(path.resolve(__dirname + '/../public/style.css')); @@ -20,6 +18,9 @@ const mainController = { }, popups: (req, res) => { res.sendFile(path.resolve(__dirname + '/../public/popups.js')); + }, + chat : (req, res) => { + res.sendFile(path.resolve(__dirname + '/../public/chat.js')); } }; diff --git a/src/public/chat.js b/src/public/chat.js new file mode 100644 index 0000000..fcbde4e --- /dev/null +++ b/src/public/chat.js @@ -0,0 +1,11 @@ +const socket = io(); +const form = document.getElementById('form'); +const input = document.getElementById('input'); + +form.addEventListener('submit', function(e) { + e.preventDefault(); + if (input.value) { + socket.emit('chat message', input.value); + input.value = ''; + } +}); diff --git a/src/public/popups.js b/src/public/popups.js index c52aea7..e78d4f5 100644 --- a/src/public/popups.js +++ b/src/public/popups.js @@ -1,7 +1,6 @@ const currentUrl = window.location.href; import { ab2str, exportedKeyToPem, pemToKey, genKey } from "./ecc.js"; - // close popups with escape key document.addEventListener("keydown", (event) => { if (event.isComposing || event.key === 'Escape') { @@ -109,8 +108,9 @@ document.getElementById("loginconfirm").addEventListener("click", async function if (!verifyResponse.ok) { throw new Error('Failed to verify the challenge'); + } else { + const verifyResult = await verifyResponse.json(); + console.log("Verification result:", verifyResult); + location.reload(); } - - const verifyResult = await verifyResponse.json(); - console.log("Verification result:", verifyResult); }); diff --git a/src/public/script.js b/src/public/script.js index 4cf056f..375e741 100644 --- a/src/public/script.js +++ b/src/public/script.js @@ -1,14 +1,2 @@ var jswarn = document.getElementById('jswarn'); jswarn.innerText = ''; - -var socket = io(); -var form = document.getElementById('form'); -var input = document.getElementById('input'); - -form.addEventListener('submit', function(e) { - e.preventDefault(); - if (input.value) { - socket.emit('chat message', input.value); - input.value = ''; - } -}); diff --git a/src/routes/root.js b/src/routes/root.js index 3e754da..ab86888 100644 --- a/src/routes/root.js +++ b/src/routes/root.js @@ -22,4 +22,8 @@ router .route("/popups.js") .get(mainController.popups); +router + .route("/chat.js") + .get(mainController.chat); + module.exports = router; diff --git a/src/views/index.pug b/src/views/index.pug index f29ee7a..4f84c23 100644 --- a/src/views/index.pug +++ b/src/views/index.pug @@ -14,10 +14,15 @@ html(lang="en-US") #mainbody #jswarn Please enable Javascript to use this app. - .btn-toolbar.btn-group-sm(role="toolbar", aria-label="Toolbar") - .btn-group.mr-2(role="group", aria-label="register") - button#register.btn.btn-secondary(type="button") register - button#login.btn.btn-secondary(type="button") login + if !isLoggedIn + .btn-toolbar.btn-group-sm(role="toolbar", aria-label="Toolbar") + .btn-group.mr-2(role="group", aria-label="register") + button#register.btn.btn-secondary(type="button") register + button#login.btn.btn-secondary(type="button") login + else + .btn-toolbar.btn-group-sm(role="toolbar", aria-label="Toolbar") + .btn-group.mr-2(role="group", aria-label="logout") + button#logout.btn.btn-secondary(type="button") logout #registerPopup.popup .popup-content