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

213
Visualizações
auth0 reactjs redirect to different route if not logged in

I'm learning Auth0 + react and am specifically looking at the example here: https://github.com/auth0-blog/auth0-react-sample

On the home.js page there is a code block:

import React, { Fragment } from "react";

import { Hero, HomeContent } from "../components";


const Home = () => (
  <Fragment>
    <Hero />
    <hr />
    <HomeContent />
  </Fragment>
);

export default Home;

What I want to do is render the JSX (? I think that's the the term) <Hero /> if the user is authenticated and render something that says "User not authenticated for this" if the user is not logged in.

I was able to get the first part working by changing the file to:

import React, { Fragment } from "react";

import { Hero, HomeContent } from "../components";
import ProtectedRoute from "../auth/protected-route";


const Home = () => (
  <Fragment>
    <ProtectedRoute path="/Hero" component={Hero} />
    {/*<Hero />*/}
    <hr />
    <HomeContent />
  </Fragment>
);

export default Home;

However I'm at a loss how to render a "Not logged in" div. I can make a separate view easily enough and make it a route but I'm lost as to how to do the if/else block?

Apologies if silly question, coming from the python world.

Thanks!

#Edit 1: The protected route component is below:

// src/auth/protected-route.js

import React from "react";
import { Route } from "react-router-dom";
import { withAuthenticationRequired } from "@auth0/auth0-react";
import { Loading } from "../components/index";

const ProtectedRoute = ({ component, ...args }) => (
  <Route
    component={withAuthenticationRequired(component, {
      onRedirecting: () => <Loading />,
    })}
    {...args}
  />
);

export default ProtectedRoute;
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Based on what you need, we can call the useAuth0 hook again in the Home component to get the auth state. Try the following:

const Home = () => {
  const { isAuthenticated } = useAuth0()
  return (
    <Fragment>
      {isAuthenticated ? <Hero /> : "User not authenticated for this"}
      <hr />
      <HomeContent />
    </Fragment>
  )
}

The useAuth0 hook is actually just a Context API consumer: https://github.com/auth0/auth0-react/blob/master/src/use-auth0.tsx

The context provider Auth0ProviderWithHistory is rendered in index.js and wraps our App component, so we can call the useAuth0 hook anywhere in our App to grab any of the Auth0 state or helper functions.

If you're not across the React Context API, here are the docs: https://reactjs.org/docs/context.html

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