I have page which can have one of 2 params. email and hash if there are not present I want to redirect the user.
/confirm-email?email=user@email.com
I have tried the following:
const router = useRouter();
const { email, hash } = router.query;
useEffect(() => {
if (!email && !hash) {
router.push('/');
} else if (hash) {
// Do stuff
}
}, [email, hash, router]);
But the problem on first render both of them are undefined
and the user gets redirected even if the hash or email params are present.
How can I fix this ? any suggestions ?
After doing some research. I have figured out it's best check if the params exists or not inside getServerSideProps()
export async function getServerSideProps({ query }) {
if (!query.email && !query.hash) {
return {
notFound: true,
};
}
return {
props: {},
};
}