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

147
Visualizações
Loading a next-auth protected page shows the wrong message while and before loading

Consider a sinple next.js page like this

const Index: NextPage = () => {

    const {data: session} = useSession();
    const [data, setData] = useState(null);
    const [isLoading, setLoading] = useState(false);

    useEffect(() => {
            setLoading(true)
            fetch('/api/myEndpoint')
                .then((res) => res.json())
                .then((data) => {
                    setData(data);
                    setLoading(false);
                });
    }, []);

    if(isLoading) {
        return <Spinner/>;
    }

    if(session) {
        return <p>here is my protected content</p>
    } else {
        return <p>you cant see this page</p>
    }
}

When I am not logged in I see "you cant see this page", then the spinner, then again "you cant see this page". And that's fine.

But when I am logged in, I see "you cant see this page", then the spinner, and then the protected content.

Same thing even if I try

if(session && data) {

How can I get rid completly of the "you cant see this page" until the page is completly loaded?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Try : if(session && !isLoading) or if(session && isLoading===false)

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