I tried to create an authentication process with ReactJS and Redux/toolkit currently.
When Client enter the page which is required auth, this program would get the value of isAuth
from store.authReducer
useEffect(()=>{
if(!isAuth) navigate("/signin", {replace: true})
},[navigate])
the authentication checker is written in the entry point of router
useEffect(() => {
dispatch(authenticateAsyncAction());
}, [dispatch]);
Suppose, client is logged successfully, and enter any page by the web app element (ex: <Link to="/user/profile">profile</Link>
), it works well.
But the problem would happen when client enter the route by browser url input field (ex:https://domain/user/profile
). The default value of isAuth
is false
, and authenticateAsyncAction()
is an async function
, the navigation will be triggered and redirect page to /signin
before isAuth
was changed to true
.
Is there any way to fix this problem?