Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

138
Vistas
Nextjs, how to redirect from _app.tsx

I am doing a routing based on the role of the user in nextjs. I did one hook which returnes true or false if a user is allowed to navigate in a route. I want to intercept that into _app.tsx file so I did the following thing:

_app.tsx

const App: React.FC<{
  Component: React.FC;
  pageProps: Record<string, any>;
}> = ({ Component, pageProps }) => {
  const router = useRouter();
  const user =
    typeof localStorage !== 'undefined'
      ? (JSON.parse(localStorage.getItem('user') ?? 'null') as
          | User
          | { role: undefined })
      : null;
  
  //useRouteProvider returns true/false
  const isUserAllowedOnRoute = useRouteProvider(router.pathname, user?.role);

  if (!isUserAllowedOnRoute) {
    window.location.replace('/');
    // I also tried router.push('/') and Router.push('/') but I can't use that into this file. 
  }
  return (
    //...
  );
};

export default App;

In this case the error is ReferenceError: window is not defined

Based on the value of isUserAllowedOnRoute, how can I redirect to a certain route from here?

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda