Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

140
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!