My attempts to set a cookie when someone inputs the correct key (1234) are resulting in an error message:
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client.
I'm confused on what steps to take next. I've tried redirecting before setting the cookie and even logging after the redirection, but the error persists!
Here is the code snippet where the key is defined:
app.get("/", function(req,res) {
if (req.cookies.userSecret == key) {
res.sendFile(__dirname + "/index.html");
} else {
res.sendFile(__dirname + "/login.html")
io.on("connection", function(socket) {
socket.on("login", function(data){
if (data == key) {
res.redirect("/1799fd8e-78aa-4bc0-9692-011a81c07248")
return false;
} else {
io.emit("log", "Wrong key! try again")
return false;
}
})
})
}
});
app.get("/1799fd8e-78aa-4bc0-9692-011a81c07248", function(req,res) {
console.log(key)
res.sendFile(__dirname + "/index.html")
})