cookie icon indicating copy to clipboard operation
cookie copied to clipboard

Cookie change events

Open rtfeldman opened this issue 7 years ago • 1 comments

I found a use case - not for reading or writing cookies, but rather detecting when they have changed.

  1. I log in, and the server uses Set-Cookie response header to set an authentication cookie.
  2. I boot with Elm.embed(elem, document.cookie), passing the cookie into flags for decoding.
  3. I'm logged in, the app shows my profile photo in the upper-right, I'm happily using the app.
  4. I open a new tab and log out.
  5. The original tab has no way to know this happened, so it still thinks I'm logged in...my profile photo is still there and everything, but now all of my requests start erroring like crazy.

In the absence of some sort of cookies API that exposed a Sub for detecting cookie changes, the only way to solve this would be ports, like so.

Since cookies are the only solution to a universal usability problem, it seems desirable for Elm to natively support cookie-based authentication with good UX when a user logs out in a different tab.

Note: A subscription to change events would not solve this without providing the cookie string itself, since we need to parse it to tell what the change was. (The UI should react differently depending on whether we got logged out versus if it changed because the server issued us a fresh authentication token, and there's no way to tell that without parsing the cookie string.)

I don't think that's an argument for adding arbitrary cookie reads, though, as I'd still think the ideal startup behavior would continue to be passing document.cookie in as a flag. That way (a) you can instead pass something from the database in for the flag when doing server-side rendering, and (b) your first render isn't blocked on waiting for a Task to complete. (Alternatively, if there were some sort of "system information" value passed to init, it could communicate the cookie info in a more reliable way.)

rtfeldman avatar May 08 '17 16:05 rtfeldman