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

237
Visualizações
How can I redirect a user to a certain page in my Next.js _app.tsx in a useEffect. Currently in infinite redirect loop

I am using Next.js and Firebase.

I want to allow users to sign up with Google, but I need to check at the app level whether the users status is "incomplete" or "active".

Here is my useEffect. I listen to userData here and if the status is "incomplete", I redirect the user to the place where I can finish their sign up process (e.g. ask for username).

useEffect(() => {
    if (currentUser && currentUser !== "unauthenticated") {
      const docRef = doc(db, "users", currentUser.uid);
      return onSnapshot(docRef, async (doc) => {
        let data = doc.data() as UserData;
        if (!data) {
          return;
        }
        if (data?.status === "incomplete") {
          router.push("/complete-user-data");
          return;
        }
        setData(doc.data() as UserData);
      });
    }
  }, [currentUser, router, setData]);

This would work however as the useEffect is constantly rerendering due to the router.push("/complete-user-data");, I am in a infinite loop.

Whats the best way of creating a top level listener that will basically block the user based on a certain condition and push them to a certain route?

Is there a way to only push to a route if the user isn't currently on that route (in my case, if they're not on /complete-user-data?)

about 4 years ago · Juan Pablo Isaza
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