Laravel From Scratch [Part 9] - User Authentication

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
alright guys so now that we have a full credit application with laravel we're going to now work on authentication and access control so if we look in our database we have a user's table because laravel ships with this migration if we go to database migrations and it creates the users table if now if you wanted to add extra fields here you could have added them here before and then ran the migration or you can even add them now and run it but this is fine for what we're going to do name email password ID so what we're going to do is we're going to enable authentication and we already have the users table so to enable authentication we can just run an artisan command so let's go ahead and do let's go to our terminal hair and all you have to do is PHP artisan make off okay this one command is going to basically enable all the the user model all the controllers the auth controllers here and we're going to have a login system that's going to just work right off the bat which is just friggin awesome so let's run that now this here when you create authentication it creates a layout for you and the layout is very similar to what we have now which is just a nav bar it uses bootstrap and it creates a form for your registration and all that well actually those are views but the layout it extends from so if we go to resources views it's going to replace this app dot bleed dot PHP file and I'm actually going to let it okay but before I do that I'm just going to copy everything that's in that file and just go to a new file and paste it in just so I have that okay because there is some stuff that we're going to have to add back and stuff all right now let's close that up and let's say yes so now if we go and let's close this and look at our app blade file and it added a bunch of stuff for us and some of this stuff we can actually use so for since it added this CSRF token which we would have had to add later anyways for security reasons it gives us a nav bar and if we look down here it has all kinds of logic so on the right side of the nav bar it's going to have a login and register link alright and it's going to check to see if we're guests if we're not logged in it'll display that else then it's going to display a drop-down with our username and also a logout function so let's go ahead and save it and go back reload and this is the navbar that is included it has a login and register link and these pages are going to work just by enabling the authentication that we did we should be able to register and all that but let's fix the nav bar there's some issues here you don't have our pages our links here anymore you also see we have some margin under it so if we go to our layout file the new one app PHP the nav bar is right in the layout and we didn't do that we had it inside of an include so what I'm going to do is copy or cut the whole nav bar out and we're going to go to our nav bar include and let's just go under all this and paste it in okay and then we'll take what we need from our old one which is this ul here with all the pages so we'll copy that and we're going to put it right above this ul that has the nav bar right okay and then we're also going to remove the static nav bar static top now if you want to go back to the black nav bar over the white one you can change this to nav bar inverse actually let yeah let's do that or you can keep it white with nav bar default okay it's up to you and let's see what else we need here let's save that make sure it looks okay we will just need to go back to our layout and include it and also we should have a container class here all right and then remember we also had the messages include so we're going to make sure we put that back all right excuse me let's try that all right so we have two nav bars now because I forgot to remove it from here so it's just actually one more thing we need to create post to link so it's just we'll cut that out and then we'll just delete the rest of this so the create post I actually only want that to show if we're logged in so let's go right let's see I'll just go right here I guess no not here not in the drop-down you know what let's just leave it out for now so it's safe and now we have our nav bar that actually makes sense so we have all of our pages and we have our login and register links so let's go ahead and try registering email address so just do brad at gmail password and register okay so what it does by default is it registers us it signs it logs us in and it brings us to /home and if we look over here in our controllers it gives us a home controller now personally I don't like the name home I would rather the speed dashboard because that tells us that it's actually a back-end you know instead of just home I think of the index page our home that the page index so what I want to do is change all of the instances of home to dashboard you don't have to do this but it's just something that I like to do so first of all is change it to dashboard controller okay and then down here it says index we're going to show a view called dashboard not home save that and actually we need to rename it so let's go over here and change home controller to dashboard controller now things are going to break because there's some other places we need to change things as well so in the auth controllers if we go to forgot password actually that's the only one we don't have to change but if we go to login controller we're going to change home slash home to slash dashboard so that it brings us to the right place after we log in same with the register controller will change this save that and also the reset password controller okay so change all those now we need to change the links to two slash home or the routes I'm sorry so we'll go down to our routes file which is where is it routes web dot PHP and notice that it automatically puts in this author routes it did that when we ran the artisan command and it also created a route to the home ok but we want to change this to dashboard and change this to dashboard controller and just going to get rid of the name here all right so let's save and now I believe everything should work I'm going to log out ok we'll go and login see something's wrong all the view we didn't change the view named so we have to go down here and change home blade to dashboard blade there we go so now we have our dashboard now what I want to do is make it so that when we create a post actually let's put a link here to create a post so we'll go to dashboard blade and let's go right into the body here and let's see what do I want to do here we're going to have an area for our posts so let's just put a heading we'll say your blog posts we're not going to do that just yet but I just want to put it there and then we'll put a link to slash post slash create and give it a class of BTN BTN primary and then let's have that just say create post okay let's check that out all right good now when we create a post I want to insert the user ID as a field now if we go to our post table we don't have that here okay we have title body but we don't have any area for user ID so what we'll do is create a migration that's going to add that to this table so let's go to artists on our command line or terminal and we're going to do PHP artisan and we want to do make migration and you want to give this a very descriptive name so we'll say add underscore user ID two posts we'll just do add user ID two posts okay so let's go ahead and run that created migration now if we go over to database migrations you'll see add user ID to coast so we're going to open that up and right now if we if we do PHP artisan migrate it's going to run this but nothing's going to happen because there's nothing in the up or down function so what we want to do is first go to the up function and we're going to say schema table I'm going to do schema table and then the name of the table we want to work with which is posts all right and then it's going to take a second parameter which will be a closer which will be a function and that's going to take in the parameter of table and then we can put we can add whatever we want here so let's say table we want this to be an integer and we want to call it user underscore ID all right so when we run the migration it's going to add this to this table now if we go down if we want to roll roll it back then we just want to delete that column you want to delete user ID so let's copy this and then instead of doing table into your user ID we're going to do table drop column user ID all right and we'll save that and now we'll go and run it so let's go to our terminal and we're going to do PHP artisan migrate okay now let's go look at our database to reload this and now you'll see that our posts have a user ID column okay so we didn't have to go into PHP myadmin or the SQL shell and do it ourselves we can just run a migration and if we wanted to undo that we could just do PHP artisan PHP artisan migrate rollback now for right now I'm just going to manually change these to the ID of one just so they belong to someone all right and if we go to our users table we have our user here okay with the idea of one now when we insert or when we create a pose so let's go over to our post controller once this we can close that up now so let's go to post controller and go to the store okay because that's where it's going to get submitted to the forum and we just want to put right here we want to add post user underscore ID now we're not setting that to a request because it's not coming from the forum but now that we have authentication enabled we can access we can use off user and then we can get any other user fields but we want the ID okay so this will get the currently logged in user and put it into the user ID and then save it so let's save that and then we want to do the same thing with the update actually no the update we don't because it's never going to change the user won't change so let's just try this out now so we'll go back to that oh we don't have a dashboard link let's we need to do that so in our nav bar which is right here and I guess we'll put it we'll put it in the drop-down so just like we have a logout here say it's actually let's grab the L I know we don't need all that let's just type it in so we'll say Li and then this is going to be a link to slash think it's just dashboard all right let's try that all right so now if we create a post let's see where we at we have one two three so let's create four whoa is the editor wait a second what happened in the editor oh the layout we got rid of it shoot so we got to go back to our app dot layout or app dot blade I'm sorry and let's see we just need to put that JavaScript back so let's go back to the documentation for a laravel - CK editor and we just need this okay put that right down here and save and that should give us the editor back good so close for submit and now if we go and look in our database and go to our post table you can see that post four got ID user ID one okay so if we were logged in with someone with the user ID of two then two would have got put there so that way we can track what posts belong to what user all right and I'm going to show you how to actually add a relationship in the model as well okay so we're going to get into that in the next video
Info
Channel: Traversy Media
Views: 290,620
Rating: undefined out of 5
Keywords: laravel, laravel auth, laravel authentication, laravel 5 auth
Id: ORus3-By4lk
Channel Id: undefined
Length: 16min 42sec (1002 seconds)
Published: Sat Jun 03 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.