Uso el almacenamiento de sesión para guardar datos de inicio de sesión como token, etc.
¿Es una buena práctica usar este escenario para redirigir a usuarios no autorizados?
useEffect(() => { if (token === null) { navigate('/users/login'); } }, []);porque tengo 2 tipos de páginas en la aplicación.
algunas páginas deberían cargarse con token y otras no
Preferiría implementar un componente que haga esta verificación por usted, como se sugiere aquí , para que no tenga que verificar el token en cada componente:
// In case authentication is required, wrap the component to be rendered inside PrivateRoute <Route path="/protectedPage" element={ <PrivateRoute> <ProtectedPage/> </PrivateRoute> } /> // In case no authentication is required, just render the element as it is <Route path="/unprotectedPage" element={ <UnprotectedPage/> } /> // In case you don't want to show the page to an authenticated user <Route path="/protectedPage" element={ <PrivateRoute redirectIfAlreadyAuthenticated={true}> <Login/> </PrivateRoute> } /> function PrivateRoute({ children, redirectIfAlreadyAuthenticated=false }) { if(redirectIfAlreadyAuthenticated) return token? <Navigate to="/" /> : children; else return token? children : <Navigate to="/users/login" />; }