[Laravel Breeze] Redirect back after login and registration

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
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 route service provider usually it's something like slash home when using larval breeze or dashboard when using gstream but in my case i set it up to be the root url 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 let's take tolpad 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 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 if we take a look inside the registered user controller 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 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 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 route service provider home let's test this out i will go into 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 let's do the same for the login flow i'll open the authenticated session controller go to the create method that will display the login form and say redirect set intended url 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 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 home so i'm going to log in again and go to authenticated session controller 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 that's how you can redirect users to the previous page before logging in or registering a new account in applications using larval breeze if you're interested in how to do the same thing with larval jetstream leave a comment and i will do a video on it it's basically the same approach but a bit different don't forget to like the video subscribe click the bell button all that stuff bye
Info
Channel: cdruc
Views: 8,707
Rating: undefined out of 5
Keywords: laravel, laravel breeze, laravel php
Id: w6DexyuWszQ
Channel Id: undefined
Length: 4min 31sec (271 seconds)
Published: Sun May 09 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.