Skip to content
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

feat(react): add refetchOnWindowFocus option to SessionProvider #3730

Merged
merged 1 commit into from
Jan 25, 2022

Conversation

timakin
Copy link
Contributor

@timakin timakin commented Jan 25, 2022

The original feature request (#3450) looks left behind for about a month.

But because you can't control whether to refetch session when you focus on a browser, some issues still occur (like form resetting, duplicated API calls, etc).

So I re-open this PR instead of @filipditrich (almost all of this feature depend on his effort) and handle @balazsorban44 's reviews.

Following descriptions are copies from here.


Adding new option refetchOnWindowFocus to SessionProvider component which disables session refetching in visibilitychange event callback.

Reasoning 💡

Automatic refetching on tab switching is usefull but it should be controllable by user, since it may be an expensive operation.

Checklist 🧢

  • Documentation
  • Tests
  • Ready to be merged

Affected issues 🎟

Fixes #3405

@github-actions github-actions bot added the core Refers to `@auth/core` label Jan 25, 2022
Copy link
Member

@balazsorban44 balazsorban44 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! 🙏 💚

@balazsorban44
Copy link
Member

balazsorban44 commented Jan 25, 2022

@timakin could you please add this to the documentation under https://next-auth.js.org/getting-started/client#options as well?

There is an edit button at the bottom, but here is the repo https://github.com/nextauthjs/docs

@hboylan
Copy link
Contributor

hboylan commented Jan 26, 2022

Thank you for implementing this feature. I need this as well since we already have a refresh token flow in our app.

Any idea when this can be published to NPM?

@timakin
Copy link
Contributor Author

timakin commented Jan 27, 2022

@hboylan
You mean you'd re-fetch the session regardless of this refetchOnWindowFocus option when you refresh an access token, right?
This package will automatically try to fetch a new access token every refetchInterval as before.
Isn't it enough?

@hboylan
Copy link
Contributor

hboylan commented Jan 27, 2022

@timakin no, I will use the refresh token to fetch a new access token and retry the service request when my service responds with 401 unauthorized. Meaning the access token is expired.

Therefore, I would like to use this new SessionProvider prop to prevent next-auth from fetching a new session and hitting the OAuth service unnecessarily

This is exactly the feature I've been looking trying to reproduce. 😄

@timakin
Copy link
Contributor Author

timakin commented Jan 27, 2022

@hboylan
Thanks for a detail. I see.
However, it looks it is not the issue of this option, but the another one of preventing useless API calls if the session was expired.
So, it's better to report as a new issue to block session re-fetch when it's already unauthorized. 👍

@hboylan
Copy link
Contributor

hboylan commented Jan 27, 2022

@timakin Basically, I would like to set refetchOnWindowFocus={false} to prevent GET /api/auth/session from firing every time the tab gains focus. This is causing several issues w/ my application. Instead, I will rely on refetchInterval or simply refresh the session if my other service responds with 401.

Any idea when this can be published to NPM?

@Code-Hex
Copy link

@balazsorban44 Hi 👋
I want to use this feature as soon as possible. When will the feature be available?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Refers to `@auth/core`
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Session revalidation on refocus causes unwanted component rerenders
4 participants