lundi 12 octobre 2020

Redirect back to previous url or to auth-protected url

In e-commerce shop, the homepage have a lot of products, and every product has a link containing its own id (/order/x). When I click on that link, I force a login action with authentication middleware. When user clicks "buy this product" I want to redirect him to /order/x after successful login. But also, if the user clicks login from the homepage navigation, I want to redirect him back to the homepage.

The case: When I try to access a route, protected by authentication middleware (in this example: /order/1), my url.intended is stored as /order/1, and that's what I want. But when I press login from the homepage that points to ), the url.intended stays at the same - /order/1 URL, unless I try to enter another auth-protected URL. I tried to use the previous URL method, but then it's not redirecting back to auth-protected routes URLs. In this case (when using url.previous) - If I press the "Buy product" button from the homepage, instead of returning to the order screen, it will redirect me back to the homepage.

What's the best and working way to redirect the user to the previous URL if it clicks on login from the homepage, or /order/1 URL if it clicks on "Buy product."



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire