react-auth-kit icon indicating copy to clipboard operation
react-auth-kit copied to clipboard

Warning: Cannot update a component (`AuthProvider`)

Open SamHoque opened this issue 2 years ago • 4 comments

Describe the bug

Warning: Cannot update a component (`AuthProvider`) while rendering a different component (`RequireAuth`). To locate the bad setState() call inside `RequireAuth`, follow the stack trace as described in https://reactjs.org/link/setstate-in-render
    at RequireAuth (http://localhost:3000/static/js/bundle.js:15439:21)
    at Routes (http://localhost:3000/static/js/bundle.js:76253:5)
    at Router (http://localhost:3000/static/js/bundle.js:76186:15)
    at BrowserRouter (http://localhost:3000/static/js/bundle.js:74995:5)
    at AuthProvider (http://localhost:3000/static/js/bundle.js:15341:21)
overrideMethod @ react_devtools_backend.js:4026
printWarning @ react-dom.development.js:86
error @ react-dom.development.js:60
warnAboutRenderPhaseUpdatesInDEV @ react-dom.development.js:27492
scheduleUpdateOnFiber @ react-dom.development.js:25498
dispatchReducerAction @ react-dom.development.js:17452
isAuth @ PrivateRoute.tsx:44
RequireAuth @ PrivateRoute.tsx:50
renderWithHooks @ react-dom.development.js:16305
updateFunctionComponent @ react-dom.development.js:19588
beginWork @ react-dom.development.js:21601
beginWork$1 @ react-dom.development.js:27426
performUnitOfWork @ react-dom.development.js:26557
workLoopSync @ react-dom.development.js:26466
renderRootSync @ react-dom.development.js:26434
performSyncWorkOnRoot @ react-dom.development.js:26085
flushSyncCallbacks @ react-dom.development.js:12042
(anonymous) @ react-dom.development.js:25651

To Reproduce Steps to reproduce the behavior:

  1. Bug happens after using signOut();

Expected behavior No error to be shown in console.

Desktop (please complete the following information):

  • OS: Windows 11
  • Browser Chrome
  • Version Latest

SamHoque avatar Aug 09 '22 14:08 SamHoque

I was not able to reproduce the bug, can you give me some codebase along with detailed steps?

darkmatter18 avatar Sep 06 '22 16:09 darkmatter18

<AuthProvider authType="localstorage" authName="_auth">
    <QueryClientProvider client={queryClient}>
        <DndProvider backend={HTML5Backend}>
            <div className="App">
                <NavigationBar />
                <NavigationMenuBar />
                <Routes>
                    <Route path="/" element={<Home />} />
                    <Route
                        path="/products"
                        element={
                            <RequireAuth loginPath={"/sign-in"}>
                                <Products />
                            </RequireAuth>
                        }
                    />
                    <Route path="/products/:id" element={<ProductDetail />} />
                    <Route path="/sign-in" element={<SignIn />} />
                </Routes>
            </div>
        </DndProvider>
    </QueryClientProvider>
</AuthProvider>

I have same bugs, when redirected by RequireAuth, then console warn this message

react_devtools_backend.js:4026 Warning: Cannot update a component (`AuthProvider`) while rendering a different component (`RequireAuth`). To locate the bad setState() call inside `RequireAuth`, follow the stack trace as described in https://reactjs.org/link/setstate-in-render
    at RequireAuth (http://localhost:3000/node_modules/.vite/deps/react-auth-kit.js?v=c3de6d14:433:21)
    at Routes (http://localhost:3000/node_modules/.vite/deps/chunk-XANHZZRR.js?v=1ade920d:1141:5)
    at div
    at DndProvider2 (http://localhost:3000/node_modules/.vite/deps/react-dnd.js?v=a35811c2:1443:9)
    at QueryClientProvider (http://localhost:3000/node_modules/.vite/deps/@tanstack_react-query.js?v=990b3ad5:2638:3)
    at AuthProvider (http://localhost:3000/node_modules/.vite/deps/react-auth-kit.js?v=c3de6d14:399:21)
    at RecoilRoot_INTERNAL (http://localhost:3000/node_modules/.vite/deps/recoil.js?v=8c47a31c:2681:3)
    at RecoilRoot (http://localhost:3000/node_modules/.vite/deps/recoil.js?v=8c47a31c:2798:5)
    at App
    at Router (http://localhost:3000/node_modules/.vite/deps/chunk-XANHZZRR.js?v=1ade920d:1088:15)
    at BrowserRouter (http://localhost:3000/node_modules/.vite/deps/chunk-XANHZZRR.js?v=1ade920d:1218:5)

say8425 avatar Sep 14 '22 09:09 say8425

Hey, I was able to fix this bug, however I don't remember how I did it.

SamHoque avatar Sep 14 '22 09:09 SamHoque

Here's a sandbox:

https://codesandbox.io/s/cocky-driscoll-82kqk0?file=/src/AppRoutes.js

The warning appears when visiting a page wrapped by RequireAuth.

ukasyah99 avatar Sep 20 '22 14:09 ukasyah99

Got the same issue here. Is there any solution yet ?

Netskill89 avatar Oct 14 '22 22:10 Netskill89

still happens to me, in fact, the example in this repo also has the same warnings.

https://github.com/react-auth-kit/react-auth-kit/tree/master/examples/create-react-app

lampard7824 avatar Dec 08 '22 13:12 lampard7824

Same happens to me. As @ukasyah99 says, it appears when you visit a paged wrapped by RequireAuth

gabohc avatar Dec 11 '22 03:12 gabohc

Any solution on this?

ghost avatar Dec 27 '22 22:12 ghost

is the issue solved?

oguisantosilva avatar Dec 28 '22 17:12 oguisantosilva

I am having the same issue, I spent around 4 hours yesterday attempting to find a work around. To the best of my knowledge. It happens in isUseAuthenticated and PrivateRoute isUseAuthenticated image PrivateRoute image

berryfd1995 avatar Dec 28 '22 18:12 berryfd1995

I am having the same issue, I spent around 4 hours yesterday attempting to find a work around. To the best of my knowledge. It happens in isUseAuthenticated and PrivateRoute isUseAuthenticated image PrivateRoute image

i fixed it using this

const PrivateRoute = ({ Component }) => { const isAuthenticated = useIsAuthenticated(); const auth = isAuthenticated(); return auth ? <Component /> : <Navigate to="/login" />; };

oguisantosilva avatar Dec 28 '22 18:12 oguisantosilva

i fixed it using this

const PrivateRoute = ({ Component }) => { const isAuthenticated = useIsAuthenticated(); const auth = isAuthenticated(); return auth ? <Component /> : <Navigate to="/login" />; };

So I attempted this before, I still received it when using react router 6 <Outlet/> and within one of the child components using useIsAuthenticated method

berryfd1995 avatar Dec 28 '22 20:12 berryfd1995

i fixed it using this

const PrivateRoute = ({ Component }) => { const isAuthenticated = useIsAuthenticated(); const auth = isAuthenticated(); return auth ? <Component /> : <Navigate to="/login" />; };

this worked for me!!

ghost avatar Dec 29 '22 13:12 ghost

Same problem here while trying to use the documentation example.

i fixed it using this

const PrivateRoute = ({ Component }) => { const isAuthenticated = useIsAuthenticated(); const auth = isAuthenticated(); return auth ? <Component /> : <Navigate to="/login" />; };

I implemented like this:

const App = () => {
    //https://github.com/react-auth-kit/react-auth-kit/issues/1023
    const PrivateRoute = ({ Component }) => {
        const isAuthenticated = useIsAuthenticated();
        const auth = isAuthenticated();
        return auth ? <Component /> : <Navigate to="/login" />;
    };

    return (
        <AuthProvider authType={'cookie'}
            authName={'_auth'}
            cookieDomain={window.location.hostname}
            cookieSecure={window.location.protocol === "https:"}>
            <BrowserRouter>
                <div className="App">
                    <Navigation />
                    <Routes>
                        <Route path="/login" element={<Login />} />
                        <Route path="/home" element={<PrivateRoute Component={Home} />}></Route>
                    </Routes>
                </div>
            </BrowserRouter>
        </AuthProvider>
    )
}
export default App

davidbro-in avatar Jan 11 '23 20:01 davidbro-in

I implemented like this:

IMHO, this is a cleaner solution:

const App = () => {
  const PrivateRoute = ({ children, loginPath }) => {
    const isAuthenticated = useIsAuthenticated();
    const location = useLocation();

    if (isAuthenticated()) {
      return children;
    }

    return <Navigate
      to={loginPath}
      state={{from: location}}
      replace
    />;
  };

  return (
    <AuthProvider
      authType={'cookie'}
      authName={'_auth'}
      cookieDomain={window.location.hostname}
      cookieSecure={window.location.protocol === "https:"}
    >
      <BrowserRouter>
        <div className="App">
          <Navigation />
          <Routes>
            <Route path="/login" element={<Login />} />
            <Route
              path="/home"
              element={<PrivateRoute loginPath="/login"><Home /></PrivateRoute>}
            />
          </Routes>
        </div>
      </BrowserRouter>
    </AuthProvider>
  );
};

export default App

kyzima-spb avatar Feb 11 '23 20:02 kyzima-spb

Was this bug fixed?

LupuC avatar Apr 11 '23 12:04 LupuC

Was this bug fixed?

i don't think so.

oguisantosilva avatar Apr 11 '23 12:04 oguisantosilva

Here the issue is, the signIn method, takes time to update the context/cookie. In between if the user gets redirected to ProtectedRoute, it fails to authenticate if (!isAuthenticated(context.authState)) {} and forces the user to sign out.

    if (!isAuthenticated(context.authState)) {
        // Redirect them to the /login page, but save the current location they
        // were trying to go to when they were redirected. This allows us to
        // send them along to that page after they login, which is a nicer
        // user experience than dropping them off on the home page.
        context.dispatch(doSignOut());
        return React.createElement(Navigate, { to: loginPath, state: { from: location }, replace: true });
    }

We are completely blocked because of this. Is there any ETA for this bug?

SaswatArabinda avatar Apr 20 '23 06:04 SaswatArabinda

I am trying my best to solve the issue. If any of you have any better solution, let me know here. It'll fast-forward the fixing the bug. Thanks

darkmatter18 avatar Apr 21 '23 15:04 darkmatter18

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Aug 07 '23 04:08 stale[bot]

had same issue

aldrinqantas avatar Dec 25 '23 21:12 aldrinqantas