next-auth icon indicating copy to clipboard operation
next-auth copied to clipboard

feat(client): add `refetchWhenOffline` option

Open balazsorban44 opened this issue 2 years ago • 4 comments

☕️ Reasoning

When the session polling is enabled (refetchInterval option), the request to /api/auth/session is made regardless of the browser network's status. If the browser is offline (i.e. computer puts itself to sleep), the request fails, and the session is invalidated (null). This behavior can cause UI issues and make other requests fail when the application stores access tokens in the session object.

This PR adds a new flag similar to the SWR's refreshWhenOffline. When disabled, all requests will be paused while the browser is offline, so the session would not be invalidated. This flag does not introduce any breaking change, but it'd make the behavior configurable by the user.

In my opinion, it would make sense to flip this to be the default behavior in the next major version.

🧢 Checklist

  • [x] Documentation
  • [ ] Tests
  • [x] Ready to be merged

🎫 Affected issues

Fixes #4935

📌 Resources

balazsorban44 avatar Jul 14 '22 23:07 balazsorban44

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Updated
next-auth ⬜️ Ignored (Inspect) Oct 23, 2022 at 3:03PM (UTC)

vercel[bot] avatar Jul 14 '22 23:07 vercel[bot]

🎉 Experimental release published on npm!

npm i [email protected]
yarn add [email protected]

github-actions[bot] avatar Jul 15 '22 00:07 github-actions[bot]

Hi folks - is there any workaround for this or ETA on when this may go in? This is an issue we are hitting often as our endusers often are mobile and get their session invalidated when offline.

viperfx avatar Sep 01 '22 14:09 viperfx

Hi folks - is there any workaround for this or ETA on when this may go in? This is an issue we are hitting often as our endusers often are mobile and get their session invalidated when offline.

You can wrap your logic in an if(navigator.onLine) { your logic }. My app would redirect to the signin page when offline because it has a redirect based on user roles. Wrapping the redirect logic in this if statement solved it for me.

Here is an example:

export default function Layout({ children }) {
  const router = useRouter();
  const { data: session, status } = useSession();
  
  useEffect(() => {
    if (navigator.onLine) {
      if (status === "unauthenticated" || session === null) {
        router.push("/first-route");
      }
      if (status === "authenticated" && session?.user?.role === "role1") {
        router.push("/second-route");
      }
      if (status === "authenticated" && session?.user?.role === "role2") {
        router.push("/third-route");
      }
    }
        return;
  }, [status, session]);

  if (status === "loading") {
    return <Spinner />;
  }
  return (
    <>
      <main>{children}</main>
    </>
  );
}

maiieul avatar Sep 06 '22 17:09 maiieul

Which next-auth version to use with this changes?

brendonco avatar Jan 31 '23 04:01 brendonco

@ThangHuuVu @balazsorban44 Hi guys! This is not working. Kindly refer to my comment in https://github.com/nextauthjs/next-auth/issues/4935#issuecomment-1606108080

nik32 avatar Jun 25 '23 14:06 nik32