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

98
Visualizações
ReactJS Authenticated Navigation

I have used react-router-dom for Navigation..but My Problem is without authentication also Dashboard Screen is being visible for mili seconds.

App.js

 <Route index path="/" element={<ProtectedRoute><Dashboard /></ProtectedRoute>} />
 <Route path="/Login" element={<Login />} />

ProtectedRoute

const ProtectedRoute = ({ children }) => {
  const { user } = useMyContext();
  if (!user) {
    return <Navigate to="/Login" />;
  }
  return children;
};

export default ProtectedRoute;

Login.js

onClick..

 await login(data.get('email'), data.get('password'));
            navigate('/', { replace: true })

Context.js

function login(email, password) {
    return signInWithEmailAndPassword(auth, email, password) 
}
function logOut() {
    return signOut(auth);
}
useEffect(() => {
    const unsubscribe = onAuthStateChanged(auth, (currentuser) => {
        setUser(currentuser);
    });
    return () => {
        unsubscribe();
    }
}, [])

How can I protect my protected screen from unauthorized access?

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

The issue is that your ProtectedRoute component doesn't wait for the authentication status to be confirmed. In other words, the default user state masks one of either the authenticated or unauthenticated status.

It should conditionally render a loading indicator while onAuthStateChanged is making the first call to determine the user's authentication status. For the initial user state value use a value that is neither a defined user object in the case of an authenticated user or null in the case of an unauthenticated user. undefined is a good initial value.

Example:

Context

const [user, setUser] = React.useState(); // initially undefined

function login(email, password) {
  return signInWithEmailAndPassword(auth, email, password);
}

function logOut() {
  return signOut(auth);
}

useEffect(() => {
  const unsubscribe = onAuthStateChanged(auth, (currentuser) => {
    setUser(currentuser);
  });

  return unsubscribe;
}, []);

ProtectedRoute

const ProtectedRoute = ({ children }) => {
  const { user } = useMyContext();

  if (user === undefined) {
    return null; // or loading indicator/spinner/etc
  }

  return user ? children : <Navigate to="/Login" replace />;
};
about 4 years ago · Santiago Gelvez 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