Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

251
Views
Couldn't route to private route with react router v6 and firebase v9

I am using firebase v9 and react-router-dom v6 in my React app. I can detect the onauthstatechanged event but I am unable to route to the private route. Here are my components: Auth:

export const AuthProvider = ({ children }) => {
  const [currentUser, setCurrentUser] = useState(null);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    onAuthStateChanged(auth, user => {
        setCurrentUser(user);
        setLoading(false);
        console.log("un auth",user);
    })
  }, []);

  if(loading) return null;

  return (
    <AuthContext.Provider
      value={{
        currentUser
      }}
    >
      {children}
    </AuthContext.Provider>
  );
};

App:

<AuthProvider>
            <Router>
                <Header />

                <Routes>            
                <Route path="/" element={<Login/>} />
                <Route path="/home" element={
                            <PrivateRoute>
                                <Home />
                            </PrivateRoute>
                        }/>
                </Routes>
            </Router>
    </AuthProvider>

So far, the onauthstatechanged event is triggering. But how can I route to private route?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Are you trying to navigate to a different route when the authstate has changed?

If that's the case, you can use

import {useNavigate} from 'react-router'

const navigate = useNavigate()
onAuthStateChanged = () => {
   ...your code
      navigate('your_private_route')
}

useNavigate is a react hook given by react-router

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!