YouTubevideo· Jan 10, 2025· 6:10· 3,800 views on YouTube
React at a moment you like. The best moments light up here.
one of the reasons why I love building new things or tinkering around with projects is I get to find new ways to solve problems and then I get to learn if that's the best way to solve it and then I get to share with you and when I put out my LC cast course about 6 months back or so I came across this problem that I had specifically with how to redirect back to a page that someone was at before they wanted to log in or register or create an account and here's
the solution I came up with is this the best solution I don't know let's figure it out okay let's say you have a project and this is the project that I built in the LC cast course if you haven't seen that it's in the description below it is a paid course through larc cast but it was so much fun building it and just learning new things mostly it's a lot of live wire a lot of Alpine but when you are in this page to listen to a podcast together if I was to go out and come
back into this it automatically starts playing but here is one neat thing that I had some trouble figuring out what's the best way to solve this if I want to click log to chat well I just it it directs me to the login P um or it takes me to the register page I should say because that's typical if you if you not if you have not created an account usually the register page is the best option well what if you have created an account um I've already registered well
now I want to log in but if I click log in where are we redirecting I mean we can't really redirect back to the dashboard that wasn't where they were last last cuz then they they let's say they go back to the dashboard then they have to click back into the page that they were in to be logged back in well here's the way that I solve this again I don't know if this is the best way to do this and there's probably easier ways to do it um but it's the way that made most
sense to me and it was actually rather okay when we're in a listening party um I want to redirect or authenticate the user now again it was it's redirecting by default to the register page because that's usually where you would go if you don't have an account within this application so I have this public function authenticate user let's see if
we can find the uh the button where we do the log to chat button we send people to we click use the wire click to authenticate user now what does this authenticate user does real quick I'm using the voltage plugin to be able to jump back up to this makes it super easy the authenticate user is going to session in the session and put this auth redirect that is specific to this route
so I'm just basically putting something into the session which is the auth redirect and I'm putting that using and again session is specific to an individual user so it's not necessarily something that's being stored in the cache for all users it's just in the session so in session we're going to put off the redirect and and the route that that user is currently on and then we're redirecting to the register route so
again what does session do well it keeps that in the session until the next page load so this is where it gets a little tricky and I think um the laravel uh session automatically does this if you're redirecting someplace that you need authentication it should redirect back but that only is for one page so if we redirect to register and we register it will automatically go back no problem but what happens if we need to click on
login and then we're logging in well redirects back to register which then you're already registered so it redirects to dashboard by default you can of course change that but how do we get to where we put this into the session well here's the way I came up with I came up with a middleware that I put in the all uh all of the apps middlewares on app.php I have this
middleware that is appended persist off redirect what What's Happening Here I'm basically just saying that if the session has an off redirect then we're going to keep it if it doesn't have an auth redirect then we're just going to return next so what's happening in the login then well we're
redirecting and we're pulling the off redirect SE section so that we can redirect them to the next place that they need to go because that's typically where the I I needed to keep that session pulled for the user because again if they go to register we're just going to automatically log them in and push them back to the where they were and I I believe I also put this in
register as well so in register um yes we're also pulling the auth redirect route and pushing that in but in login I'm pulling it as well and that's the only two places that I need it because that's my authentication pages so again this is probably extremely simple extremely basic but it's something that I didn't necessarily think of and I had to do a little bit of digging into okay what am I actually trying to do and what
I was trying to do is keep this specific route in this case a specific listening party route I wanted to keep that available so that I know knew where to direct the US so maybe you have specific projects where you're learning stuff like this you're learning okay I need this particular action to happen but how am I going to make that work and yes maybe this isn't the best use case maybe there are specific things that are in the LEL framework that I don't know
about maybe there are things that could be in the laral framework that would be great to solve this particular problem of having to log in but I'm already registered so I want to actually register and of course you could fix this with UI to say log in to chat or maybe register to chat and then you're not having to figure out which one is which but I wanted to make it as easy easy as possible for the users and that middleware made it easy so whatever you're doing whatever you're learning
whatever you're building maybe you can find a little thing like I did so keep creating