Redirecting back after login and registration in applications using Laravel Breeze
[00:02] By default, Laravel redirects users to the home route after they log in or register a new account. Whatever "home" means can be changed in the
RouteServiceProvider.php; usually it's something like '/home' when using Laravel Breeze or
/dashboard when using Laravel Jetstream; but in my case I set it up to be the root url.
[00:25] However sometimes it makes sense to redirect the user back to the page they were on before they logged in or registered a new account.
[00:34] Let's take tallpad as an example. I am on an episode page, and I want to see the code snippets for this episode, but here it says I need to create a new account in order to do so.
[00:45] So I click the free registration button and fill in my details. Then I press the register button and I'm redirected back to the home page. Ideally I should be redirected back to the episode page I was on. So let's make it happen.
[01:02] If we take a look inside the
RegisterdUserController and go to the store method, we could try to redirect the user back, but if we think about it, "back" is actually the registration page, not the episode page. So redirecting back won't work. What would work is to try and redirect to the intended url. But currently Laravel has no idea what that url is. So we need to set it before we end up on the store method.
[01:39] And we can do that inside the create method. Here we can do something like redirect, set intended url and we need to set the previous url. So url, previous. So now when we hit the registration endpoint that calls the create action, we instruct Laravel to remember the previous url as the intended url. Then when the user submits the form and the store method is called, we tell Laravel to redirect to the intended url. ANd we can also set the home route as a default here: so `RouteServiceProvider::HOME.
[02:20] Let's test this out. I will go into the browser and log out, go to an episode, register a new account, press the registration button, and I'm redirected back to the episode and I can visit the code snippets.
[02:43] Let's do the same for the login flow. I'll open the
AuthenticatedSessionController, go to the create method that will display the login form, and say
redirect()->setIntendedUrl(url()->previous()) and then on the store method I want to make sure that we'll redirect to the intended url; which we are already doing. Let's test it out in a browser.
[03:13] I'm going to log out, go to an episode code, login, and we are redirected back. Let's do the same for the logout because currently we are also redirected to
/home. So I'm going to log in again and go to
AuthenticatedSessionController, here on the
destroy() method I want to redirect to the previous url. I can do here like so:
url()->previous(). And now if I logout, I am redirected back to the episode, which is the intended behavior.
[04:07] That's how you can redirect users to the previous page before logging in or registering a new account in applications using Laravel Breeze. If you're interested in how to do the same thing with Laravel Jetstream, leave a comment and I will do a video on it. It's basically the same approach but a bit different.