Redirect to the Previous URL after logging in
Answered
Egyptian Mau posted this in #help-forum
Egyptian MauOP
I have a login page (/login) and I want it to redirect the user to the previous page after finishing logging in. In case the previous page is not a site route (for example, user directly entered the URL example.com/login into the search bar), then I want to redirect it to root page (/).
I thought of using next/headers to get the referer URL and redirect to it, but since the login page is a client component (as it uses useState and Router), I can't use next/headers in it.
Another approach could be to use useRouter().back(), but I need to check if the previous page was a site route or not, which is not possible with this function.
What should I do to achieve this?
I thought of using next/headers to get the referer URL and redirect to it, but since the login page is a client component (as it uses useState and Router), I can't use next/headers in it.
Another approach could be to use useRouter().back(), but I need to check if the previous page was a site route or not, which is not possible with this function.
What should I do to achieve this?
Answered by Egyptian Mau
Nevermind I figured it out.
Using middleware is not possible afaik since Nextjs has set the referrer policy to
I will have to modify each link to the /login route to redirect to their respective pages.
Using middleware is not possible afaik since Nextjs has set the referrer policy to
strict-origin-when-cross-origin and hence will not return any referrer, and hence no way of getting the source URL.I will have to modify each link to the /login route to redirect to their respective pages.
7 Replies
you can do it like every other big company does it: add a return url url parameter. Like .../login?redirect_url=/hello
and when the client itself go to /login there is no redirect_url so you redirect to /
and when the client itself go to /login there is no redirect_url so you redirect to /
@B33fb0n3 you can do it like every other big company does it: add a return url url parameter. Like .../login?redirect_url=/hello
and when the client itself go to /login there is no redirect_url so you redirect to /
Egyptian MauOP
But I already have the Link and redirects defined at various places in the code. Is there any way I can add the param without changing each one of my redirects?
I am thinking to use middleware but not sure if it's the best way
well, I am not that into your code, so I don't think that I can help you the best ...
Egyptian MauOP
Nevermind I figured it out.
Using middleware is not possible afaik since Nextjs has set the referrer policy to
I will have to modify each link to the /login route to redirect to their respective pages.
Using middleware is not possible afaik since Nextjs has set the referrer policy to
strict-origin-when-cross-origin and hence will not return any referrer, and hence no way of getting the source URL.I will have to modify each link to the /login route to redirect to their respective pages.
Answer
Egyptian MauOP
Thank you for the help
ok. If solution, please mark