Skip to content

Avoid stale headers by supplying them as a function #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
inad9300 opened this issue Mar 8, 2023 · 2 comments
Open

Avoid stale headers by supplying them as a function #33

inad9300 opened this issue Mar 8, 2023 · 2 comments

Comments

@inad9300
Copy link

inad9300 commented Mar 8, 2023

One of the most common usages for headers is authentication. However, if one provides a token as part of a header to this library, its value will remain the same forever, even through retries and whatnot. If at a given moment the token expires and needs to be refreshed, there will be no hope in reconnecting, so the retry mechanism becomes quite pointless.

I propose to allow passing "headers" in as a function, in which case it will be called every time fetch() is, in order to recompute the potential dynamic values in them.

@jgarplind
Copy link

jgarplind commented Apr 21, 2023

For anyone in need of a workaround, if you check for 401 in onopen, something like:

if (response.status === 401) {
  throw new AuthenticationError();
}

you can catch that outside of the fetchEventSource-call, get a fresh token, and then call fetchEventSource again.

Something like:

setUpConnection()
  .catch(err) {
      if (err instanceof AuthenticationError) {
        // Try again with a new token
        setUpConnection();

where setUpConnection grabs a token and then calls fetchEventSource.

@whawker
Copy link

whawker commented Jan 12, 2024

I've added a headers as a function PR here #67 FWIW

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants