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

Add Dev Login Route0:00

We're finally to the point to where we can focus on things that require authentication. And I think the great place to start will be the ArtistToFollow component. Now, we don't have a UI for signing in, so I think what we are going to do is just have a couple of routes that we can use to sign in and sign out. And the first is gonna be dev/login for well logging in. And this doesn't need to be fancy.

well logging in. And this doesn't need to be fancy. Uh, all we really need to do is grab a random User because ultimately it, it really doesn't matter at least for the sake of, you know, testing and making sure everything is working here. So we're gonna get the first random User so that we can log in that User, but I think it would be best if we, uh, reset the session or, or rather regenerate the session.

but I think it would be best if we, uh, reset the session or, or rather regenerate the session. And then after we do that, uh, let's redirect somewhere. And I think it would be great to just redirect to the profile that is now signed in. Uh, so we want the route profiles.show, and then we want to provide the profile for the User. And that will be that as far as signing in, uh, it helps if $request is correct, we need use statements for the User and Auth.

Add Logout Route1:22

it helps if request is correct, we need use statements for the User and off. And, you know, if we have a login, we need to log out. So this is, well, I'm not gonna say it's easier, but in this particular case, we just sign out, we don't want to regenerate here, we want to invalidate, and then we will regenerate the token so that then we will redirect. We don't want to go back to the profiles. So what do we do? Um, you know what? Let's do this.

We don't want to go back to the profiles. So what do we do? Um, you know what? Let's do this. We will redirect to /feed. That way, whenever we sign out, uh, we will just display the feed. And the feed doesn't require people to be signed in because if no one is signed in, then we'll just grab, you know, the latest posts and, and put them in the feed. So yeah, I think that's gonna be great, except that this needs to be logged out, not log in.

Switch Component to Profiles2:11

So yeah, I think that's gonna be great, except that this needs to be logged out, not log in. So we can log in, we can log out. So we're great there. Uh, let's open up the Artist to follow components. We also need the view because we need to make some changes. You know, the, all of this was just kind of put into place, you know, until we had something actual to use. And of course, right now our artists are a, a static array. And I wanna change this. Instead of artists, we'll use profiles.

And I wanna change this. Instead of artists, we'll use profiles because that's in the grand scheme of things. That's what we are going to be displaying. So let's first start off with the profiles that we want to show when the user's not signed in. So in this case, we're gonna grab some random users. We want four. Do we want four? Yes, we want four. We will take four of those and then we will get them so

We will take four of those and then we will get them so that then we will supply those profiles to our view. And we will need to, uh, make a few changes to the view so that we iterate over our profiles as a profile. And then our image is different. It's that avatar, URL. So we'll have profile avatar, URL, I don't know, what do we want to do as far as the alt of this?

So we'll have profile avatar, URL, I don't know, what do we want to do as far as the alt of this? Do we want to use the handle or the name? I, I guess we should use the display name. I don't remember what we did in, you know, the other views. Uh, but we will go with display name here. If that's not what we did elsewhere, we'll change it, but that'll be fine. And then here we want the handle. So we have the avatar, URL, the display name and the handle.

Handle Signed-In Suggestions3:45

And then here we want the handle. So we have the avatar, URL, the display name and the handle. So if we take a look in the browser, sure enough, we get four random profiles. Every time we refresh, we get four random ones. That's awesome. So now we just need to change that so that whenever we are signed in, then we have something different. And to start. Uh, okay, so this is our profile. We are following six profiles.

Uh, okay, so this is our profile. We are following six profiles. So really what we are going to do here is we want to select the profiles that, uh, Cody Mitchell currently doesn't follow. But we also really need seven profiles because we also don't want to follow Cody Mitchell again. And, and I know that the code isn't going to allow us to do that, but I don't want to even display that as an option.

And, and I know that the code isn't going to allow us to do that, but I don't want to even display that as an option. So what we will need to do is first of all, check if the user is authenticated. So we'll call auth()->check(), we'll have an else. And then let's go ahead and put this code here. Uh, we'll clean this up a little bit, but I want to keep this separate. I, I know that we could do all of this within just one query and there is something to say about that.

I, I know that we could do all of this within just one query and there is something to say about that. However, you know, taking this approach to me, it's much more clear if the User signed in, then we're gonna do this query. If the User isn't, then we're gonna do this query and, and you know, we can simplify it some, but that's generally what I wanna do. So that's what we're gonna do. And the first thing we are going to do is get our User so

So that's what we're gonna do. And the first thing we are going to do is get our User so that we can get the profile, but then we want to get our list of profiles, but we want the profiles where doesn't have followers. And then we are going to have a closure to build a query where the followerProfileID is profileID. But that's just part of it.

ID is profileId. But that's just part of it. We also want where the ID is not this profileId. And let's also just get these in random order and eventually we're gonna take four. But for right now, I just want to get them all just so that we can see the difference between being signed in and being signed out. So now we have a list of profiles.

and being signed out. So now we have a list of profiles and these are all the profiles. So what do we have? We are following six. And then if we include the current profile, that's seven. So we should have 13 over here. So 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13. So we're golden there. All we need to do then here is take four and then we're good to go.

Refactor Profile Query6:29

All we need to do then here is take four and then we're good to go. But I do want to get rid of some of this repetition such as taking and getting, so that we would do something like this to where we will build a query and here we will have our query so that we, we select the profiles we currently aren't following, or we get just the random profiles so that then we can have profiles equals we'll call query in.

or we get just the random profiles so that then we can have profiles equals we'll call query in randomOrder because you know, we have that up here. So let's get rid of that so that this would just be query so that then we can call randomOrder. Then we want to take four and get them. So we eliminate some repetition there and it's still pretty clear as to what's going on. If we are signed in, we get, you know, the profiles that we want.

If we are signed in, we get, you know, the profiles that we want. Otherwise we just get some random ones. And so now we're signed in. It doesn't matter if we refresh, we're gonna get four different, or I should say four random profiles. But of course if we sign out with dev/logout, we are taken back to feed, which that works. And if we refresh, you know, I, I don't remember what

Wrap Up and Next Steps7:38

we are taken back to feed, which that works. And if we refresh, you know, I, I don't remember what that profile was, but that's okay. So now that we have the ability to sign in, we can focus on more functionality such as making a post. And we will take a look at that in the next episode. I.

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