iron-session
iron-session copied to clipboard
Accessing the request object
I'm using next-iron-session on a project that uses dynamic routes, and I'd like to be able to dynamically set the cookie name based on the route being accessed.
Can I access the request object in the withIronSession
function? That way I could grab the req.headers.host
property.
Hey @josiahwiebe. I just published a brand new version of iron-session, see here: https://github.com/vvo/iron-session/releases/tag/v6.0.0
About your question, for now it's now feasible but we could easily add it and it would also solve other issues maybe. For example we could allow cookieName to be a method that would be called right before the cookie is serialized/unserialized and you would have to pass your options there.
You would also access the req/res object inside it.
It would look like this:
withIronSessionApiRoute(function (req, res) {
}, {
password: "...",
cookieName: function(req, res) {
return `cookieName${req.somethingComputed}`
}
})
Let me know what you think. I would also appreciate you having a look on how to implement that. You can clone the repository, add a test and see how it goes. Thanks!
Thanks @vvo! I updated right away, was fairly seamless. Thanks for the release!
I'll take a look at creating a PR on that soon. I figured out an alternate solution for now, but still wouldn't mind solving it. I don't have a tonne of TS experience so it will be a good exercise!
I figured out an alternate solution for now,
How did you do it?
As for contributing you could start duplicating this test here: https://github.com/vvo/iron-session/blob/bd98c524d9aefaf6d5a87d4836e4adcbcb4c121d/src/index.test.ts#L83-L113
Don't bother too much about TypeScript for now, you can put // @ts-ignore vvo will fix
in front of any line that fails for you
And:
- duplicate the test and add change test( to test.only(
- pass different defaultOptions with a cookieName being a function
- fiddle around this line:
const seal = cookie.split(";")[0].split("=")[1];
to get the cookieName being sent on the header (right now it strips down everything to get only the cookie value) - run
npx jest
to see the test failing
Once you have a failing test you can try implementing it.
Or if you're not a fan of TDD you can try to update the example/next.js
and pass a cookieName of a function. To develop locally you will have to run npm link
inside the root of the project and then npm link iron-session
inside the root of the example/next.js. To watch and build the library, run npm run watch
at the root of the project.
This feels very complex I agree I need to add instructions on how to contribute :)
It's possible to do something like this to be able to dynamically set the cookie max-age? Would be handy to set the max-age based of the "remember me" flag from the login form to create a session or a persistent session cookie.
@ricardotx Yes we could use the same technique for maxAge computation (allow passing a function for cookieOptions)
@vvo it would be great! Thank you.
@ricardotx Yes we could use the same technique for maxAge computation (allow passing a function for cookieOptions)
+1 for this feature!
Any update on the maxCookieAge being able to change dynamically?
iron-session v8 solves this since you have to get the session yourself, 👍