I'm using react redux with Next.js and I want to redirect the user to a certain page if he matches the requirement. I added it in my useEffect and it works, but every time when I go back to the home page it redirects again to the same page. It works fine If I refresh the page which is going to clear the redux state.
useEffect(() => {
const validateUser = (services: ServiceType[]) => {
if (services.length === 0) {
router.push('/dashboard/expert/onboard');
} else {
router.push(
'/dashboard/expert/[expertname]?view=overview',
`/dashboard/expert/${userProfile.expertProfile.name}?view=overview`
);
}
};
if (userProfile) {
if (userProfile.isVerified) {
if (nextPath && nextPath.as) {
router.push(nextPath.href, nextPath.as);
} else if (userProfile.isExpert) {
setModalVisible(false);
if (services) {
validateUser(services);
}
// setAlreadySignedUpModalVisible(true);
} else if (loginResponse && loginResponse.expertApplications.rejected > 0) {
setModalVisible(false);
setApplicationRejectedModalVisible(true);
} else if (loginResponse && loginResponse.expertApplications.pending > 0) {
setModalVisible(false);
setApplicationReceivedModalVisible(true);
setIsPendingExpert(true);
} else {
if (nextPath) {
router.push(nextPath.href);
}
setModalVisible(false);
}
} else {
setModalVisible(false);
setEmailVerifyModalVisible(true);
}
}
}, [
loginResponse,
setAlreadySignedUpModalVisible,
setApplicationReceivedModalVisible,
setApplicationRejectedModalVisible,
setEmailVerifyModalVisible,
setModalVisible,
userProfile,
nextPath,
setIsPendingExpert,
services,
]);