Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

137
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda