connect to socket on login

This commit is contained in:
2025-02-16 19:44:39 +01:00
parent 2073b81bbe
commit 156a39f7cb
8 changed files with 31 additions and 36 deletions

View File

@@ -1,26 +1,9 @@
const crypto = require('crypto');
const database = require("../db");
const authentication = require("../authentication");
const socket = require('../socket').default;
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
}
// Set cookie
res.cookie("user", randomNumber, options);
console.log("cookie set");
}
res.redirect('/');
},
register: async (req, res) => {
try {
const { sharedSecret, publicKey } = req.body;
@@ -40,6 +23,7 @@ const accountController = {
}
},
loginGetChallenge: async (req, res) => {
//crypto.randomBytes() instead of Math.random() for cryptographically secure random numbers
let randomBuffer = crypto.randomBytes(16);
let randomNumber = randomBuffer.toString('hex');
req.session.randomNumber = randomNumber;
@@ -56,6 +40,8 @@ const accountController = {
let validKey = authentication.verifySignature(msg, sig, publicKeys);
if (validKey !== null) {
req.session.publicKey = validKey;
socket.auth = { validKey };
socket.connect();
return res.status(200).json({ message: "Challenge solved successfully" });
} else {
return res.status(400).json({ error: "Challenge failed" });

0
src/controllers/chat.js Normal file
View File

View File

@@ -3,7 +3,6 @@ const path = require('path');
const mainController = {
root: (req, res) => {
let pubKey = req.session.publicKey;
console.log(pubKey);
let isLoggedIn = typeof pubKey !== 'undefined';
res.render('index', {isLoggedIn, pubKey});
},