pug different view if logged in or out

This commit is contained in:
2025-02-12 18:44:30 +01:00
parent b547fc3b5e
commit 5cfb29131c
7 changed files with 69 additions and 60 deletions

View File

@@ -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');

View File

@@ -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'));
}
};