react-oidc-context icon indicating copy to clipboard operation
react-oidc-context copied to clipboard

Endless retries when authority / authorization server is not responding

Open tinnakaren opened this issue 10 months ago • 6 comments

Not sure if this is a bug or some configuration I'm missing, but is there a way to stop the withAuthenticationRequired hoc from retrying (seemingly endlessly) to call the authorization server when there's a network error (or if the server isn't responding)?

I have requestTimeoutInSeconds setting set to 3, and if I'm e.g. on the login page (which calls auth.signinRedirect), the useAuth hook is able to reach the oidc client, which respects the timeout and returns with an error "Failed to fetch".

But if I'm on a route that's protected with the withAuthenticationRequired hoc, it goes into some redirecting loop, and tries to reach the authorization endpoint repeatedly.

Image

tinnakaren avatar Feb 05 '25 22:02 tinnakaren

i am seeing the same thing, it needs state on whether its previously checked auth or not

a1dutch avatar Feb 14 '25 10:02 a1dutch

i am seeing the same thing, it needs state on whether its previously checked auth or not

Thanks for replying @a1dutch 🙂

Could you elaborate a bit on that or show an example of what you mean? Do you mean to implement something similar to what is done for automatic sign-in? (https://github.com/authts/react-oidc-context?tab=readme-ov-file#automatic-sign-in)?

tinnakaren avatar Feb 14 '25 11:02 tinnakaren

yes i was thinking something like the example you posted. i was seeing 90k requests in the network tab!!!

i have been testing a fix locally (a cut down version) see below, it could be improved to try n times or maybe include a setTimeout.

export const withAuthRequired = <P extends object>(
  Component: React.ComponentType<P>
): React.FC<P> => {
  const displayName = `withAuthRequired(${Component.displayName || Component.name})`;
  const C: React.FC<P> = (props) => {
    const auth = useAuth();
    const [ hasTriedAuth, setHasTriedAuth ] = useState(false);

    React.useEffect(() => {
      if (hasAuthParams() || auth.isLoading || auth.activeNavigator || auth.isAuthenticated || hasTriedAuth) {
        return;
      }
      setHasTriedAuth(true);
      void (async (): Promise<void> => {
        await auth.signinRedirect();
      })();
    }, [auth.isLoading, auth.isAuthenticated, auth, hasTriedAuth]);

    return auth.isAuthenticated ? <Component {...props} /> : null;
  };

  C.displayName = displayName;

  return C;
};

a1dutch avatar Feb 14 '25 16:02 a1dutch

I would accept such a pull request, including try n times or maybe include a setTimeout using defaults but overridable via props...

pamapa avatar Mar 03 '25 16:03 pamapa

This just caused us a big headache today as well. Is there any workarounds?

animalillo avatar Jul 11 '25 12:07 animalillo

One workaround for auto sign in is to check your auth server's health first. Example.

zach-betz-hln avatar Jul 11 '25 14:07 zach-betz-hln