Take a look at my protected route for unauthenticated pages:
import React, { useEffect } from 'react'
import { Route, Outlet, Navigate } from 'react-router-dom'
import { Login, SignUp } from '../pages'
import useVerifyToken from '../hooks/useVerifyToken'
const InauthenticatedRoutes = () => {
const isAuth = useVerifyToken()
return !isAuth ? <Outlet /> : <Navigate to="/"/>
}
const routes = [
<Route key="3234" path='/' element={<InauthenticatedRoutes />}>
<Route key="6757" path='login' element={<Login />}/>
<Route key="67567" path='signup' element={<SignUp />}/>
</Route>
]
export default routes
The reason this file exported an array is because this will later be iterated inside the Routes element
the useVerifyToken is something like this
function useVerifyToken() {
const jwt = localStorage.getItem('access')
let data = 0
useEffect(() => {
....verifying token
}, [window.location.href])
return !data ? 0 : 1;
}
As you can see inside the useVerifyToken hook there's a useEffect that has a window.location.href as a dependency this is because I want this hook to verify the token every time the user switch in between pages. But the problem here it's that if I went from the route Login to Signup which is nested under the same parent, the useVerifyToken wouldn't be triggered. Personally I can't really find any work around without radically changing the route structure.
My question
How do I trigger the useVerifyToken to run even if I have to access a route under the same parent route? if not then what kind of work around would you recommend