0

I'm using Restify module to create REST server and using Restify-session module to save user credentials. However, I cannot seem to retrieve save session data. My request object does not have session data I saved earlier. I tried 'load' method of session object and data parameter of the callback function and it only contains SID. And seems that Restify does not have much resources online. I couldn't find much online. I appreciate much if anyone can point me to the right direction. This is my attempt

restify = require('restify'),
session = require('restify-session')({
    debug : false,
    ttl : 60            // Time to live in seconds
});
server  = restify.createServer();
server.use(session.sessionManager);
server.listen(8001);

server.post('/session', start);
server.post('/homepage', loadHomePage);

function start(req, res, next) {    
   var data = {user: req.query.credential}; // Base-64 encoding of username and password                

   session.save(req.session.sid, data, function(err, status){
       if(err) {
           console.log("Session data cannot be saved");
           return;
       }        
       console.log("status-" + status + data.user); //debug code
   });

function loadHomePage(req, res, next) { 

   session.load(req.session.sid,function(err,data){
       if(err) {
           res.writeHead(440, 'Login Time Out', {'Content-Type': 'application/json'});
           res.end("{'Status': 'Error', 'Message': 'Session expired or does not exist'}");
           return;
       }
       for(var n in data) { // Checking what properties data has
          console.log(n);   // Shows only SID value
       }

});

UPDATE : I found out what's going out. Restify-Session is generating new session id for every request. That's why I cannot find saved data. But I'm not sure how to resolve this issue.

KMC
  • 1,677
  • 3
  • 26
  • 55

1 Answers1

0

I found the culprit. The problem is I did not set cookies, so I was basically using newly generated ID for every request to load session data. Restify-session is doing what it suppose to do - generate sid for every request. My newbie mistake.

KMC
  • 1,677
  • 3
  • 26
  • 55
  • Hi, Any chance to get updated code example ? How to set a cookie and avoid restify-session to create new sid every request Thanks – Igal Jul 03 '17 at 12:46
  • Looking at the source (https://github.com/mgesmundo/restify-session/blob/master/lib/restify-session.js), you simply need to pass in a header "session-id" (assuming you are using default settings) in your request with the value being the one retrieved from the original call. – pkid169 Aug 23 '17 at 06:51