hapijs / yar

A hapi session manager
Other
133 stars 59 forks source link

I can set but I can't get #108

Closed omidh28 closed 7 years ago

omidh28 commented 7 years ago

I using yar as my session plugin along with catbox-redis. This is my configurations:

const Hapi = require('hapi');
const yar  = require('yar');
const hapiServer = new Hapi.Server({
  cache: {
    engine: require('catbox-redis'),
    host: redisAddress,
    port: redisPort
  }
});

hapiServer.connection({
    host: hapiHostAddress,
    port: hapiPort
});

hapiServer.route({
  method: 'GET',
  path: '/test/{key}/{value}',
  handler: function (request, reply) {

      // In MongoDB, this data will be stored in the
      // "!yar" collection within the "hapi-cache" database.
      var data = {};
      data[request.params.key] = request.params.value;
      request.yar.set('example', data);
      return reply(request.yar.get('example'));
  }
});

const yarOptions = {
    storeBlank: false,
    maxCookieSize: 0,
    cache: {
      expiresIn: 24 * 60 * 60 * 1000
    },
    cookieOptions: {
        password: 'the-password-must-be-at-least-32-characters-long',
        isSecure: true
    }
};

hapiServer.register({
  register: yar,
  options: yarOptions
}, function (err) {

  // start your hapiServer after plugin registration
  hapiServer.start(function (err) {
    console.log('info', 'Server running at: ' + hapiServer.info.uri)
  })
})

The problem is that request.yar.get('example') returns the correct data but it doesn't use redis (I'm monitoring redis with redis-cli monitor) even though request.yar.set('example', data) saves the data to redis.

Shouldn't it fetch from redis catch instead?

devinivy commented 7 years ago

At a glance your configuration looks good. The cookie data should be retrieved from redis during onPreAuth if the yar cookie session id is sent with the request. The cookie data is only written to redis during onPreResponse. Between onPreAuth and onPreResponse the cookie data is maintained in memory, which is why you don't see yar.get() reaching out to redis.

omidh28 commented 7 years ago

@devinivy Yeah the problem was that cookies were disabled in requestJs, Thanks!

lock[bot] commented 4 years ago

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.