تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Auth Redirect Problem0:00

Okay, I'm going to start closing out this series, but in the next few videos, let's work on some minor features. So the first one has to do with auth redirects. Right now I'm not logged in. And if I were to log in, and there are several ways to do that. We have these buttons up here. We can click on the vote button. We have these buttons here, or we have these buttons here. Then let me log in here. It's going to redirect us back to the homepage, which I don't want to happen.

Then let me log in here. It's going to redirect us back to the homepage, which I don't want to happen. What I want to have happen is I want to be redirected to the page I was on before I logged in. So let's go ahead and do that. So let me log out again. Go back to this page here. And like I said, we have to do this in several places, but we'll start with the vote button here. So I believe this is an IdeaController show, I have that open already.

Using Intended URLs0:49

here. So I believe this is an Idea show, I have that open already. And right here in the vote method, let me change this to auth()->guest() instead of !auth()->check(). It's a bit cleaner. And I'm going to change this redirect to use redirect()->route() instead. And the param is the route name. Same thing, just a bit cleaner, in my opinion. So Laravel has this concept of an intended URL. So if we were to take a look at the Redirector class, you can see the full path there.

So Laravel has this concept of an intended URL. So if we were to take a look at the Redirector class, you can see the full path there. And quick note, if you don't see files within the vendor folder or the node_modules folder, and you do want to see those files, and if you're using VS Code, let's go to your settings file and make sure to set this setting. So search.useIgnoreFiles to false. So if it's true, then it's going to ignore some folders in your .gitignore file, like the vendor folder or the node_modules folder. And usually I like to look at code within Laravel core. So I turn this off.

And usually I like to look at code within Laravel core. So I turn this off. Okay, so let's go to that Redirector class. And we have this setIntendedUrl method, which we'll make use of. And as you can see, all it's doing is putting a session variable name URL::intended. And if we make use of the intended method right here, then it tries to find that and then tries to redirect to it. So now we are using Laravel Breeze. So let's take a look at the controller that handles login, or the class. And it's this one, I believe, right here.

So let's take a look at the Controller that handles login, or the class. And it's this one, I believe, right here. And we are making use of that intended URL. So if that session variable exists, then go ahead and redirect there. If not, then redirect here. So I want this to happen on registering as well. So let me copy this registering is here. And instead of this, I'm going to use this. Okay. So let's close this, close this.

Setting Intended URL2:48

Okay. So let's close this, close this. And all we have to do is call that method. So we can do redirect, setIntendedUrl, and give it a path. So the path I want to give it is url()->previous(). And by the time we hit this route, then url()->previous() will be this page in this case. So this URL. Okay, so let's try that out. So let me save this. Let's go here.

So let me save this. Let's go here. Let's refresh. So it should redirect back to this page after we log in. So I believe it shows in debug bar as well. So let me open that. There's a session tab here. And as you see, the intended session variable is set to the previous URL that we're on. So let me log in. And after I do, we should be redirected back to this intended page.

Creating Redirect Trait4:04

And we can just copy and paste this or I am going to make a trait instead. So let's go ahead and do that. Let's make a traits folder. And let's make a new file in here called WithAuthRedirects.php. Okay. And let's open that up. Let's give it a namespace App\Http\Livewire\Traits. And it's a trait WithAuthRedirects. Okay. And we're just going to have a method here.

Okay. And we're just going to have a method here. Let's call this one redirectToLogIn. Okay. And we can describe what we did here. So this and paste it in here. And that should be good. Sorry. I spelled trait wrong. Okay.

I spelled trait wrong. Okay. So let's save that. Instead of using this, we can use the trait. So let's use it up here. I named it useWithAuthRedirects. And if I press tab, it should import it does. And now we can just make use of it in here and anywhere else that we need it. So here we can just call that method that we just made. So it was called redirectToLogin.

So here we can just call that method that we just made. So it was called redirectToLogin. Okay. So now we can use this in our IdeaIndex. Same thing. Wherever that code is. Again, I'm going to change this to auth()->guest() instead. And we can just use that trait up here. Use WithAuthRedirects imports it. And we can do the same thing here.

Let's go back to the index page instead. Let's go to AV page three. And if I hit vote, am I logged out? I am. If I hit vote here, you'll see that the intended URL is set correctly, and it should redirect back to this page after we log in. So again, let me log in. And it does redirect to the correct page. Cool. So we have to add it here as well in the reply.

Applying Across Components7:18

But for anything else here, I want it to redirect to that page that we were on. So we have to do it for this as well. And for sign up, let's see if that works too. So I forgot what this component is called. So an Idea shows what we have here. That was called AddComment. So let's go to that. And the logic for showing this is in the view. So let's go to that instead, AddComment and look for please log in. Okay.

So let's go to that instead, add comment and look for please log in. Okay. So it's right here. And here's where we're redirecting to the login page or the register page. So let's do this instead. Let's do wire:click.prevent. And we can call that method on the trait we made. So redirect to login. And for register, I'm going to create a new one called redirectToRegister, which is basically the same thing, but it redirects to the register page.

And for register, I'm going to create a new one called redirectToRegister, which is basically the same thing, but it redirects to the register page. Okay. So let's go to our trait and let's add one for redirectToRegister. So let's duplicate this redirectToRegister. And it's the same thing. We want to set the intended URL as well, but it redirects to the register page. Okay. And back to the AddComment class. Let's make sure to import that trait.

And back to the AddComment class. Let's make sure to import that trait. So use withAuthRedirects. Okay. Let's try this out. Let me refresh. So again, if you look at the bug bar, you'll see that there is no url.intended session variable. Let's try register. There you see it appear here.

And it does. Cool. So again, let me log out here and you can give the same treatment to this over here. So that would be in createIdea. So let's go to that. Let me grab this actually. createIdea. And let's make sure to use that trait. So up here, use. Make sure to import.

So up here, use. Make sure to import. And the logic is within the view. So we can do the same thing we did for the other component. Actually, I don't think the logic is in this component. Let's check the layout. So app.blade.php and see if it's in here. So yeah, it's in here. So yeah, the logic is in here. As you see, we have this @auth directive.

So yeah, the logic is in here. As you see, we have this auth directive. And if we are logged in, go ahead and render that component. But if not, then render all of this stuff here. So I'm going to move this within the component so we can do the same thing we did for the last component. So let's grab all of this. Let's cut it out. And we don't need the directives in here, but we do need them in the component. Okay, let's just remove these spaces.

And we don't need the directives in here, but we do need them in the component. Okay, let's just remove these spaces. And let's go to that component, createIdea. So I think I need another root div here. So let's wrap this in a div. And this case is if we're logged in. So say auth. And let's re-indent this. And then when we're not logged in, we can do else. And we can do endauth here.

And then when we're not logged in, we can do else. And we can do end auth here. And let's paste in that code. Okay. Hopefully, this still functions the same. Okay, but now we can do the same thing we did. So we can do... Where is it? Let's just copy it from the AddComment component right here. So right click prevent.

Let's just copy it from the add comment component right here. So right click prevent. Same thing. And same thing here. Redirect to register. Okay, so let's try that out. So intended URL is not set in here. Let's go to one of these. If we log in, it should set it, and it does. And if we actually log in, it should redirect us back accordingly.

If we log in, it should set it, and it does. And if we actually log in, it should redirect us back accordingly. Okay, cool. And register should work as well. I think there's one more spot in here. So again, we log out. When we try to hit myIdeas. So let's try that out. That should be in ideasIndex. And it should be somewhere here.

That should be in ideas index. And it should be somewhere here. So yeah, right here. So again, I'm gonna change this to auth()->guest() instead. And let's make use of that trait up here. So use this and with auth redirects. Okay. And we can use that method here. This redirect to logIn. Cool.

Running Tests and Commit13:21

in one video. Sorry, almost forgot to make a commit here. Actually, let's make sure our tests still pass. Okay, cool. So git add, this is episode 65, git commit, episode 65, auth redirects.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟