import './App.css';
import { BrowserRouter as Router, Routes, Route, Navigate } from 'react-router-dom';
import { useEffect, useState } from 'react';
import MyHome from './components/MyHome';
import Header from './components/Header';
import Footer from './components/Footer';
import CommentList from './components/CommentList'
import PostComment from './components/PostComment';
import SignUp from './components/SignUp';
import SignIn from './components/SignIn';
import EmailVerification from './components/EmailVerification';
import Cookies from 'universal-cookie'
import NavigationBar from './components/NavigationBar';
import Takeaway from './components/Takeaway';
import TakeawayChinese from './components/TakeawayChinese';
function App() {
const [logInState, setLogInState] = useState(false);
const cookies = new Cookies()
const handleLogout = () => {
setLogInState(false);
cookies.remove('Token');
localStorage.clear();
}
const checkTokenExpiration = () => {
const cookiesToken = cookies.get('Token')
const localStorageToken = localStorage.getItem('Token');
const expirationTime = localStorage.getItem('TokenExpiration');
if (cookiesToken && localStorageToken && expirationTime) {
const currentTime = new Date().getTime();
if (currentTime > Number(expirationTime)) { handleLogout(); }
else { setLogInState(true); }
}
};
useEffect(() => {
checkTokenExpiration();
const interval = setInterval(checkTokenExpiration, 1000);
return () => clearInterval(interval);
}, []);
return (
<div className="App">
<NavigationBar logInState={logInState} handleLogout={handleLogout} />
<Header />
<Routes>
<Route path='/' element={<MyHome />} />
<Route path="/commentlist" element={<CommentList logInState={logInState} />} />
<Route
path="/postcomment"
element={
logInState ? (
<PostComment />
) : (
// <Navigate to="/signin" />
<SignIn />
)
}
/>
<Route
path="/signup"
element={
logInState ? (
<Navigate to="/" />
) : (
<SignUp />
)
}
/>
<Route
path="/signin"
element={
logInState ? (
<Navigate to="/" />
) : (
<SignIn setLogInState={setLogInState} />
)
}
/>
<Route path="/emailverification/:token" element={<EmailVerification />} />
<Route path='/takeaway' element={<Takeaway />} />
<Route path='/takeawaychinese' element={<TakeawayChinese />} />
</Routes>
<Footer />
</div >
);
}
export default App;
"/postcomment"
route is not working properly:
<Navigate to="/signin" >
After logged in, the above route takes the user to home page instead of postcomment page.
But it works fine if i change to the below route
<SignIn >
Does anyone know why Navigate
component is causing the bug?
or are there better ways of doing it if I need to use a log-in state to control the routes?
2
Answers
i noticed its the bootstrap components causing the bug, the postcomment navbar link works if i used original react tags (the commented out section), even if i use Navigate component in app.js file does anyone know why?
It’s possible that the logInState update is asynchronous and the navigation is triggered before the state is updated properly. This can happen if the login state update is delayed or if there’s a race condition.
Then, in your main App component, you can use the ProtectedRoute wrapper like this: