mozilla / node-client-sessions

secure sessions stored in cookies
Mozilla Public License 2.0
759 stars 105 forks source link

add beforeUpdate hook #122

Open ponelat opened 7 years ago

ponelat commented 7 years ago

Hi,

My use case is to synchronize two cookies, one plaintext and the other a secret. This hook allows me to update the plaintext cookie, just as the secret one gets updated.

Would love to get this merged, but don't mind alternatives. If any spring to mind.

seanmonstar commented 7 years ago

So, this runs a possible callback when the cookies are serialized? And the reason to do this in your case is to so that the 2 cookies are synchronized, and you don't want to forget to update the plaintext cookie accidentally?

Could this also be achieved with another middleware and override res.writeHead again?

ponelat commented 7 years ago

@seanmonstar thanks for the prompt response. Some assumptions:

  1. We don't have access to rawSession
  2. clientSession will only write the cookie, when rawSession#isDirty

I think decorating writeHead again isn't a bad idea. But based on the assumptions above, I would need to write my plaintext cookie on every request, as apposed to when clientSession writes its cookie. Since I don't know when the rawSession is dirty ( and needs to send a new cookie ).

Hence, I just stuck a callback into client session code. So I could know exactly when clientSession was going to write its cookie, and I could do something about, it before the headers were sent.

seanmonstar commented 7 years ago

If the session was dirty, you'll see the Set-Cookie header in res.getHeaders(), right?

ponelat commented 7 years ago

@seanmonstar good point! I'll see if I can't make a workaround with that in mind, thanks!