hapijs / yar

A hapi session manager
Other
133 stars 59 forks source link

Assign data in onPreResponse lifecycle extension with takeover #144

Closed suhanw closed 4 years ago

suhanw commented 4 years ago

Support plan

Context

How can we help?

Hi! I'm fairly new to yar and hapi, but let me do my best to explain what I'm trying to accomplish:

const getBase = async (request, h) => { let alerts = request.yar.get('alerts');

// other code

}


But for some reason, the `alerts` in session did not persist from `onPreResponse` to the homepage pre-handler, i.e., `request.yar.get('alerts') === null` in the pre-handler. Is there something about `onPreResponse` lifecycle that doesn't allow me to save data into session? Any help is greatly appreciated. 

Below are my `yar` options. I tried to distill the code snippets to just the relevant parts, but let me know if I can provide any further info that might help. 
```js
{
    plugin: require('yar'),
    options: {
        name: nconf.get('redis:cookieName'),
        maxCookieSize: 0,
        cache: {
            expiresIn: nconf.get('redis:ttl')
        },
        cookieOptions: {
            ttl: nconf.get('redis:ttl'),
            password: nconf.get('redis:secret'),
            isSecure:
                process.env.NODE_ENV === 'production' ||
                process.env.NODE_ENV === 'staging' ||
                process.env.NODE_ENV === 'ephemeral',
            isHttpOnly:
                process.env.NODE_ENV === 'production' ||
                process.env.NODE_ENV === 'staging' ||
                process.env.NODE_ENV === 'ephemeral'
        },
        storeBlank: false
    }
},

Many thanks!

hueniverse commented 4 years ago

What is happening first, yar registration or your custom onPreResponse ext setup?

suhanw commented 4 years ago

They are both plugins included in the same array, but yar appears first in the array:

const plugins = [
    // other plugins
    {
        plugin: require('yar'),
        options: {
            name: nconf.get('redis:cookieName'),
            maxCookieSize: 0,
            cache: {
                expiresIn: nconf.get('redis:ttl')
            },
            cookieOptions: {
                ttl: nconf.get('redis:ttl'),
                password: nconf.get('redis:secret'),
                isSecure:
                    process.env.NODE_ENV === 'production' ||
                    process.env.NODE_ENV === 'staging' ||
                    process.env.NODE_ENV === 'ephemeral',
                isHttpOnly:
                    process.env.NODE_ENV === 'production' ||
                    process.env.NODE_ENV === 'staging' ||
                    process.env.NODE_ENV === 'ephemeral'
            },
            storeBlank: false
        }
    },
    // other plugins
    { plugin: require('./routes/main') },
    // other plugins
];

await server.register(plugins)

require('./routes/main') is the plugin where the onPreResponse ext setup lives.

hueniverse commented 4 years ago

You need to call the new await request.yar.commit(h) before returning the redirect.

suhanw commented 4 years ago

Is this a new feature that's not available in yar@9.1.0?

hueniverse commented 4 years ago

Only in 10.1.0.