pug different view if logged in or out
This commit is contained in:
@ -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');
|
||||
|
@ -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'));
|
||||
}
|
||||
};
|
||||
|
||||
|
11
src/public/chat.js
Normal file
11
src/public/chat.js
Normal file
@ -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 = '';
|
||||
}
|
||||
});
|
@ -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);
|
||||
});
|
||||
|
@ -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 = '';
|
||||
}
|
||||
});
|
||||
|
@ -22,4 +22,8 @@ router
|
||||
.route("/popups.js")
|
||||
.get(mainController.popups);
|
||||
|
||||
router
|
||||
.route("/chat.js")
|
||||
.get(mainController.chat);
|
||||
|
||||
module.exports = router;
|
||||
|
@ -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
|
||||
|
Reference in New Issue
Block a user