beginner here. I have the following code which gives access to various pages depending on the presence of a user and him being an admin.
const [currentUser, setCurrentUser] = useState(undefined);
const [isAdmin, setIsAdmin] = useState(false);
useEffect(() => {
const user = AuthService.getCurrentUser();
if (user) {
setCurrentUser(user);
if (user.isAdmin)
setIsAdmin(true);
}
}, []);
<Route exact path={"/"} element={<Home props={currentUser} />} />
<Route exact path="/login" element={!currentUser ? <Login /> : <Navigate to='/' />} />
<Route exact path="/watch/:id" element={isAdmin ? <Watch /> : <Navigate to='/login' />} />
<Route exact path="/movies" element={<Movies />} />
<Route exact path="/admin" >
<Route index element={isAdmin ? <AdminPage /> : <Navigate to='/' />} />
<Route path="moviespanel">
<Route index element={<Moviespanel />} />
</Route>
Now, the conditions in 4 first routes work just fine. the one in index element does not work when isAdmin variable changes from false to true. It does not work in any different way i write it. isAdmin === true ? ... : ... etc etc
Bear in mind that the same condition is in Route /watch and there it works perfectly fine. Does it matter that it is in an index element ?
I tried to use isAdmin && which works for both conditions but i do not know how to get it to Navigate to "/" when isAdmin is false.
... Edit,
I actually found out that the condition works if I follow the paths through buttons/links, they do not work when I type in the url directly in the browser.
Is there something in the ternary operator "?" that i do not know ?