Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

208
Views
SOCKET.IO: Access cookies from devtools. Express-session

Why can't I access this cookie?enter image description here

When the user logs in the cookie is recieved and sent back to the Express server.

When initializing a new websocket to the Socket.io server this cookie does not get sent, so I was trying to get it via the document.cookie. However, it did not work since the the cookie was not modifiable.

enter image description here

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

It is an HttpOnly cookie that cannot be accessed via client-side Javascript.

In other words: The server is able to read and manipulate the cookie. The client receives it and blindly sends it back with every subsequent request, without being able to read or manipulate its contents, at least not with Javascript means.

about 4 years ago · Juan Pablo Isaza Report

0

The official website did something that did not work for me. express session middleware

const session = require("express-session");

io.use(wrap(session({ secret: "cats" })));

io.on("connection", (socket) => {
  const session = socket.request.session;
});

So, I got around it by making: Logic:

Before establishing websocket, request credentials to express endpoint "/userCredentials"
and then use these credentials to establish the connection

Warning: Down below code is stripped because I did so many auth logics

CLIENT: 
...
useEffect(() => {
  (async() => {
    const pending_creds = await fetch("/userCredentials");
    const creds = pending_creds.json();
    
    const ws = io({auth: {creds}})
    
    setSocket(ws)
  })()
}, [])

...

SERVER:


...
app.get("/userCredentials", (req,res) => {
  const userSession = req.session.user;
  
  res.json(userSession)
})
...

io.use(socket, next){
  const creds = socket.handshake.auth.userSession;
  if(creds){
    next()
  } else {
    socket.disconnect()
  }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!