Authorize Tweet Creation0:00
Okay, now that we have Auth in place, let's update some of the features that require a logged in user. So the first one is adding new tweets. So if you remember, here is the endpoint for adding new tweets, and we are just hard coding user1 here. So obviously we want this to be the logged in user. So let's do that. We just have to add the middleware. So let's grab it from here. Let's add it here to our endpoint.
So let's grab it from here. Let's add it here to our endpoint. And instead of hard coding one, we can do either request user_id, or we can just do Auth::id() or just Auth id will work as well. And in our front end, we have to make sure to pass in the token. So let's go back to our front end. Let's go into our newTweet screen, newTweet. And we have to pass in the token before we make this call. So let's do that. So I have that token in the setting screen where we're logging out.
So let's do that. So I have that token in the setting screen where we're logging out. So I'm just going to grab it from there. Actually, it's in our Auth provider. So let's go to that Auth provider, and it should be in our logout method. So let's grab this, which attaches the bear token for the logged in user. Let's paste that in here. And we have to make sure to grab the user too from our context. So let's grab that import as well from here. So that should be this import right here.
So let's grab that import as well from here. So that should be this import right here. Actually, this is the context, so we can't grab it from here. Let's just do it manually. So we need to make use of use context. So let's grab the User. And let's use context, let's import that. And we are grabbing it from the Auth context. Import that. Okay.
Import that. Okay. I forgot the = sign. And that should be it. Okay, let's go ahead and save that. And hopefully I did everything correctly. So now let's log in as one of the users here. Let's try userTwo. So this person right here, let me just make sure this is the correct data. So when we log in as this user, add a new tweet, we should see the tweet from this user.
So this person right here, let me just make sure this is the correct data. So when we log in as this User, add a new Tweet, we should see the Tweet from this User here. So Dr. Elisa. So let's try that. So let's log in. Okay, let's add a new Tweet. New Tweet from the Dr. Tweet. And it is correct. So that is working.
Add Followed Tweets Feed2:36
And it is correct. So that is working. Okay, next for our list of tweets here, we're just listing all the tweets. So let's take a look at that endpoint in our backend. So as you see here, we're just listing all the tweets. So now that we have a logged in User, this should be a list of tweets of the people that they follow. So we have to implement following in our backend. So let's go ahead and do that. So we need a follows table.
Create Follows Migration2:59
So let's go ahead and do that. So we need a follows table. And the keys are going to be the two user IDs. So the person following and the person being followed. So let's start with that. So let's go into our backend. Let's make a new migration php artisan make:migration, say create_follows_table. Okay. And let's go ahead and go into that. create_follows_table.
And let's go ahead and go into that. Create follows table. Okay. And let's go ahead and add our primary key. So the primary key is going to be a combination of those two userIds. So let's say userId, and let's call it followingUserId. Okay. And both of these have to reference the user table. So say table for an id. And the first one is userId.
So say table for an ID. And the first one is user_id. And let's make it constrained. So references the users table, and it's just hard code users here to make sure it does reference the users table. Let's say on delete cascade as well. Okay. And we have to do the same for the other field. So for an ID for the following user_id, and that should be good. Okay.
So for an ID for the following user_id, and that should be good. Okay. Now we have to add the relationship on the User model. So let's do that. It's going to be a belongsToMany relationship. So let's define a new method called follows. And like I said, it's going to be a belongsToMany relationship. So this belongsToMany User. Okay. And the table is going to be follows.
Okay. And the table is going to be follows. And we need the two keys. So let's specify user_id. And the other one is called following_user_id. Okay. So let's go ahead and save that. Let me just log out here, since we're going to php artisan migrate:fresh. And let's go ahead and do that. So php artisan migrate:fresh --seed.
And let's go ahead and do that. So php artisan migrate:fresh --seed. Okay, so that worked. And now we should have that new follows table in our database. And we have to refresh this to there it is. So this will have information about which User follows which User. So for example, if User one follows 20 Users, we'd have 20 entries in here. And this will all be User one. And then this will show all the people they follow. So let's actually create some fake data.
Seed Users and Follows5:30
And then this will show all the people they follow. So let's actually create some fake data. So let's go into our database/seeder. And let's add some data in here. So now we only have these 40 tweets that we're creating, which also creates 40 users. But for our fake data, I just want to be able to easily identify if the list of tweets is correct. So let's create the 100 users here. So I'll stick with this one here. And then I'm going to define 99 more so we can have 100 users.
So I'll stick with this one here. And then I'm going to define 99 more so we can have 100 Users. So it's a UserFactory 99. And I'm going to make a sequence here. Just so each person's name is person 1 to person 100. User 1 is going to be me. So that should be easily identifiable. And then the rest of the people will have person 2 to person 100. So let's say function, let's make a sequence here. And let's give it a name or give them a name.
So let's say function, let's make a sequence here. And let's give it a name or give them a name. And let's just name them person with the index at the end. So let's say person. And then we'll append the index here. So sequence, sequence, index, okay, plus two, actually, so we get the correct number. And let's say create. And then we remove the tweets for now. And let's just see if I did this right. So php artisan migrate:fresh --seed.
And let's just see if I did this right. So php artisan migrate --seed. So open up our database, check out our users. And we do have person2 to person100 here. Okay. Okay, so here's what I'm going to do. For each group of 20 people, I want that group to all follow each other. So everyone within this group will all be following each other. So let's go ahead and work on that. So we'll just use loops here.
So let's go ahead and work on that. So we'll just use loops here. So for each, say range, 1 to 20, let's say as $userId. And let's make another loop in here. Let's just copy this actually. And let's say $userId2 here. And we just want everyone to follow each other. So we'll say User::find($userId) and we'll grab that relationship we made so follows. And we'll add the other $userId2 so that they all follow each other.
And we'll grab that relationship we made so follows. And we'll add the other user_id so that they all follow each other. So User::find(2). Okay. Sorry, there should be user_id. And hopefully I did this right. So let me save this. php artisan migrate:fresh --seed again. Check out our database. And let's check out the follows table.
Check out our database. And let's check out the follows table. Okay. So User 1 follows everyone within that group. There we go. And so on and so forth. So User 2 follows everyone and all the way up to User 20. Okay, cool. And let's also make sure that each User has one Tweet. So let's just add a Tweet in here or tweet factory.
And let's also make sure that each User has one Tweet. So let's just add a Tweet in here or TweetFactory. So TweetFactory::create. And the user_id is going to be this user_id. Okay. So one more time, this should generate sorry, this should be in square brackets. And it should generate 20 Tweets since I'm looping over 1 to 20 here. Okay. One more time. Let's check our database.
Filter Tweets by Following9:28
So we should have 100 tweets and we should have what 2000 follows and we do. Cool. Okay. Now let's update our tweets endpoint. We don't want to show all the tweets. We only want to show the tweets of the users that this person follows. So I actually want to keep this, but just change the route name. So let's change to tweets.all and we have to update this. So we do need the loggedInUser. So we do need that middleware, but we'll add that in a second as we formulate our Eloquent
So we do need the logged in User. So we do need that middleware, but we'll add that in a second as we formulate our Eloquent query here. So let's just pretend that the logged in User is the first User. And remember, everyone is following 20 Users, including themselves. So we need a list of this person's followers. So let's just do followers and we can grab this User's followers. So User, we can use that follows relationship and this will be a collection and we're only interested in their IDs so we can pluck their ID. Okay.
interested in their IDs so we can pluck their ID. Okay. And let's just dd that. Okay. And let me just hit this endpoint in our browser. Okay. So we do get this collection and it should be just their IDs and it is cool. So now we want to limit this query to only these followers here. So we can do that with the whereIn method. So I'm going to add that here.
So we can do that with the whereIn method. So I'm going to add that here. So whereIn, and it's going to be the userId and we have that list of followers, but just to make it clear, let's just hard code [1, 2, 3] as an array. So 1, 2, 3. Okay. And this should only return the tweets, forgot a closing bracket here, and it should only return the tweets of these three users. Okay. So let's try this out in the browser.
Okay. So let's try this out in the browser. Let me just comment this out. So one, two, and three all have only one tweet. So this should return three tweets, hopefully if I did it correctly. So let's refresh this. And it looks like we do have three tweets here. So if I scroll down, it looks like there are three. So we just have to pass in the list of followers here instead of this hard coded array. And that should do the trick.
So we just have to pass in the list of followers here instead of this hard coded array. And that should do the trick. So followers. And if we hit this again, it should be 20 tweets now, because we're only listing the tweets of the people that this person follows. So let's try that one more time should be 20 and it is cool. So let's update this. So it makes use of the logged in user. So this is going to be auth user. And we have to add that middleware to get rid of this.
So this is going to be auth User. And we have to add that middleware to get rid of this. And let's add this middleware here. And we'll clean up the controller eventually. Everything is still in the routes file. But this should work for now. Okay. Save that. And now we have to update our front end to make sure that we pass through the token when we're retrieving our tweets.
And now we have to update our front end to make sure that we pass through the token when we're retrieving our tweets. So let's do that. So that should be on the home screen. So where are we retrieving the tweets right here. So we're doing it twice, actually. But the endpoint is the same. So we just have to make sure to pass in the token. So we can grab that token from the new tweet screen. We also need this auth context.
So we can grab that token from the new tweet screen. We also need this auth context. Actually let's do this first. So let's add this as an import up here. Or down here. Let's make sure to import. Use context. And auth context. Okay. And let's grab the bear token.
Okay. Cool. So hopefully I did everything correctly. Let's go ahead and log in as a User here. Let's log in as a first User. And it looks like everything does work. So if you look at the tweets here, we are only getting tweets from the people we follow. And remember, person1, who I'm logged in as right now, only follows everyone from 1 to 20. So these tweets only show 1 to 20.
Add Search All Tweets14:43
Let's paste this in. Okay. Let's change the name to searchScreen here. Okay. And let's just change the endpoint to tweetsAll. And we don't need this Axios config here or the passing in of the token. So let's get rid of that. Let's get rid of this. And let's change the endpoint here. So we have to change the tweetsAll and tweetsAll here.
And let's change the endpoint here. So we have to change the tweetsAll and tweetsAll here. Okay. Let's save that. And now hopefully the search screen should show all the tweets. So if you go here, it looks like it does. Cool. And you can see we're getting tweets from people we don't follow as well. Okay. So everything seems to be working correctly.
Okay. So everything seems to be working correctly. I think we have to add one more thing here. So when we create a new User, we want to make sure that we follow ourselves after we create that User. So that's just on the back end here. So let's go back to our Laravel app. Let's go to our routes file. And right here where we're creating a new User, let's just make sure that we follow ourselves.
And right here where we're creating a new User, let's just make sure that we follow ourselves. So we can do user follows, and then we can attach ourselves. Okay, hopefully that should work. Let's go ahead and try that out in our REST client. So the endpoint is here. This information is already filled out. Let's make sure that's correct. Okay. So that worked.
Okay. So that worked. That's 101 for the ID. Let's make sure this person follows themselves. So let's refresh this. Let's look for user 101. And they do follow themselves. Cool. So now we're making use of the loggedInUser here. And we are pulling the correct information based on their userID.
So now we're making use of the logged in User here. And we are pulling the correct information based on their userId. So we updated this screen to only show tweets from the people we follow. And we also updated the new tweet screen here. So let's go ahead and make a commit as always. We'll work on following and unfollowing people in the next video. So there should be changes on the front end and back end. So for the front end, let's say git commit -m "update features for logged in user". And for the back end, let's say git add git commit -m "add following or follows logic".
And for the back end, let's say git add git commit add following or follows logic.
