0

Am using express-session in my project its working fine, if user logged in to my site means I stored their data in session and after logout destroyed session, thats also working fine, after logout if user run particular url means that will take user to that page without log in, need to restrict that one, below is my tried code

  app.use((req,res,next)=>{
  if(!req.session.data)
  {
      return res.redirect("/"); or res.redirect("/"); //Both not working
  }
  next();
  })

this shows the below error

Error: Can't set headers after they are sent.
    at validateHeader (_http_outgoing.js:491:11)
    at ServerResponse.setHeader (_http_outgoing.js:498:3)
    at ServerResponse.header (/home/djaxtech/Documents/luka-asset/node-app/node_modules/express/lib/response.js:767:10)
    at ServerResponse.send (/home/djaxtech/Documents/luka-asset/node-app/node_modules/express/lib/response.js:170:12)
    at done (/home/djaxtech/Documents/luka-asset/node-app/node_modules/express/lib/response.js:1004:10)
    at tryHandleCache (/home/djaxtech/Documents/luka-asset/node-app/node_modules/ejs/lib/ejs.js:257:5)

Anyhelp appreciated..!

Thiyagarajan
  • 247
  • 3
  • 5
  • 15
  • Somewhere prior to this part you are already sending sth to the header as said 'Can't set headers after they are sent'. So it's hard to tell without knowing what the script looks like before you call this part. – Philipp M Aug 03 '18 at 10:10
  • am just using simple script only, if user run every url in browser I need to check wheather user in session or not ? so that am using app.use middleware , if user not in session mean I will redirect user to index page , that code are app.get("/",(req,res)=>{ res.render("index")}); – Thiyagarajan Aug 03 '18 at 10:23

2 Answers2

0

The code below is a middleware:

Using middleware

What does middleware and app.use actually mean in Expressjs?

This means that there is another function that would be executed in the line when next() is called. and you must be sending out response from there as well.

And below, the error means, the response is already sent and you are trying to send again which is not possible.

Error: Can't set headers after they are sent.

app.use((req, res, next) => {
    if (!req.session.data) {
        // return res.redirect("/"); or res.redirect("/"); //use only one.
    }
    next();
})

Using session

req.session To store or access session data, simply use the request property req.session, which is (generally) serialized as JSON by the store, so nested objects are typically fine. For example below is a user-specific view counter:

// Use the session middleware
app.use(session({ secret: 'keyboard cat', cookie: { maxAge: 60000 }}))

// Access the session as req.session
app.get('/', function(req, res, next) {
  if (req.session.views) {
    req.session.views++
    res.setHeader('Content-Type', 'text/html')
    res.write('<p>views: ' + req.session.views + '</p>')
    res.write('<p>expires in: ' + (req.session.cookie.maxAge / 1000) + 's</p>')
    res.end()
  } else {
    req.session.views = 1
    res.end('welcome to the session demo. refresh!')
  }
})
Harshal Yeole
  • 4,812
  • 1
  • 21
  • 43
0

Please try this

app.use((req,res,next)=>{
  if(!req.session.data)
  {
      return res.redirect("/"); or res.redirect("/"); //Both not working
  } else {
    next();
  }
})
Raj Jaril
  • 376
  • 1
  • 16