Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
September 25, 2022 02:05 pm GMT

After Login redirect Back to post where you are reading without login

Hi Guys
Back with another real-life scenario in software development.

Do you know when you are scrolling to some famous websites like LinkedIn, Facebook, etc without logging in?

And when you want to comment or like a post but that requires a login and After login, the website will take you to the same post or Article

How did this happen?

It is actually very easy to do,

just append the URL of the post as a query parameter in the login URL.

const goToLoginPage=()=>{
router.push(
/login?redirectTo=${router.asPath});
}

And in the login API function just checks if the URL contains this param and then send the user to this route

async function loginUser(){
const { redirectTo } = router.query;
const res=await loginApi(); ;
if(res.success){
router.push(redirectTo ?? '/');
}
}


Original Link: https://dev.to/shacodes/after-login-back-to-post-where-you-are-reading-without-login-1ln2

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To