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

136
Vistas
How to get dynamic page's id

I have a HOC which redirects user to login if there is not token. And what i want to achieve is to store url which user intended to visit before redirecting and push him back to that page. But the problem is in HOC I get only /product/[id] but not /product/1.

withAuth.ts :

import { TokenService } from '@/api/tokenService';
import { useRouter } from 'next/router';
import React from 'react';

type TWithAuth = {
    WrappedComponent: JSX.Element;
};

export function withAuth<T extends TWithAuth = TWithAuth>(
    WrappedComponent: React.ComponentType<T>,
): React.ComponentType<T> {
    const InnerComponent = (props: T) => {
        if (typeof window !== 'undefined') {
            const Router = useRouter();
            console.log(Router);
            const accessToken = TokenService.getAccessToken();

        if (!accessToken) {
            Router.replace('/auth/login');
            return null;
        }

        return <WrappedComponent {...props} />;
    }

    return null;
};

InnerComponent.displayName = `withAuth(${WrappedComponent.displayName || WrappedComponent.name}`;

return InnerComponent;
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I'm assuming you are not using server-side rendering (getServerSideProps). Then, you need to persist state between requests on the client-side (browser)

These are the steps:

  • When a user lands on the page and is not logged in, persist the original page request to a cookie or local storage.
  • Redirect to login
  • Login user.
  • When a user is logged in successfully, check the cookie,
  • If there is a cookie (or local storage), take the data (original URL)
  • Delete the cookie
  • Redirect to the original page.
about 4 years ago · Juan Pablo Isaza Denunciar

0

You are calling the useRouter hook inside an if statement, which breaks the first rule of hooks.

Also,

Try:

export function withAuth<T extends TWithAuth = TWithAuth>(
    WrappedComponent: React.ComponentType<T>,
): React.ComponentType<T> {
    const InnerComponent = (props: T) => {
        const Router = useRouter();
        useEffect(()=> console.log(Router), []);
        const accessToken = TokenService.getAccessToken(); // is it async? If so, maybe it should be wrapped inside useEffect

        if (!accessToken) {
            Router.replace('/auth/login');
            // no need to return null, you are already redirecting
        }

        return <WrappedComponent {...props} />;
    }
    return null;
};
about 4 years ago · Juan Pablo Isaza Denunciar
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