iron-session icon indicating copy to clipboard operation
iron-session copied to clipboard

Accessing the request object

Open josiahwiebe opened this issue 3 years ago • 8 comments

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.

josiahwiebe avatar Nov 03 '21 13:11 josiahwiebe

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!

vvo avatar Nov 04 '21 15:11 vvo

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!

josiahwiebe avatar Nov 04 '21 20:11 josiahwiebe

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:

  1. duplicate the test and add change test( to test.only(
  2. pass different defaultOptions with a cookieName being a function
  3. 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)
  4. 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 :)

vvo avatar Nov 04 '21 20:11 vvo

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.

rteixeirax avatar Nov 05 '21 11:11 rteixeirax

@ricardotx Yes we could use the same technique for maxAge computation (allow passing a function for cookieOptions)

vvo avatar Nov 05 '21 11:11 vvo

@vvo it would be great! Thank you.

rteixeirax avatar Nov 05 '21 12:11 rteixeirax

@ricardotx Yes we could use the same technique for maxAge computation (allow passing a function for cookieOptions)

+1 for this feature!

claysam75 avatar Mar 07 '22 16:03 claysam75

Any update on the maxCookieAge being able to change dynamically?

searchableguy avatar Mar 12 '22 23:03 searchableguy

iron-session v8 solves this since you have to get the session yourself, 👍

vvo avatar Nov 20 '23 00:11 vvo