Senica Gonzalez
Senica Gonzalez

Reputation: 8192

SailsJS session variable not able to be set on first page load

This is the controller:

rusty = require('rusty')

module.exports =
  index: (req, res)->
    console.log('submit', req.session)
    res.json({})

  captcha: (req, res)->
    _ref = rusty.captcha({
      width: 120
      height: 50
      chars: 'abcdefghijklmnopqrstuvwxyz0123456789'
      length: 4
      fonts: ['20px sans', '20px bold sans']
      noise: 50
    })
    canvas = _ref.canvas
    code = _ref.code
    req.session.captcha = code
    return canvas.toBuffer (err, buffer)->
      return res.end(buffer)

I have a route to captcha.png that calls the captcha method here. An image src is set to captcha.png. I expect that when the captcha method is called, req.session.captcha is set and I can use it to verify the form in the index method.

However, on first page load after restarting the server, the session variable is not set. If I do a subsequent call to the captcha method, then the session variable is set and can be used.

I'm assuming this has to do with the session cookie not set yet? Or is there something else I'm overlooking?

Any thoughts?

Upvotes: 2

Views: 79

Answers (1)

Senica Gonzalez
Senica Gonzalez

Reputation: 8192

Ah, just a development issue. The fact that I already had a cookie set from a previous session, would invalidate the session on the request. Effectively, storing the variable under the wrong session. If I delete my cookie after a server restart, then all is well on page refresh.

Leaving here for anyone else scratching their head.

Upvotes: 2

Related Questions