How Does Authentication Works In Laravel? | Laravel Authentication Course | Laravel Mailing Course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up laravel developers is dari here and i hope that you're having a great day now that we just finished the larval api course i think it might be good to dive into authentication and mailing in laravel this is a pretty big step forward if you think about the projects that you can build in laravel if we take an e-commerce as an example you offer products to the customer and let's say that they can also subscribe to your newsletter if they want to order a product they need to be authenticated since you need their personal information and if you want to send them a notification or an email of their order you obviously need mailing now before i continue on with the video i want to quickly let you know that you can support a channel through patreon which is linked in the description down below you get pretty cool benefits such as a private discord group where everyone is helping each other out and you can decide the next video series that i'm going to make for the polls so if you're interested to join the link is down in the description when i created my laravel for beginners series which is also linked in the description down below i added a chapter at the end of the course about authentication and authorization not knowing that you could make an entire course about it i want to cut all the small talk and introduction videos and how to set up a project and i actually want to dive right into the code now i have a folder on my desktop where i store my laravel projects called workspace but i also created a shortcut command called cdw which will change directories to the workspace so let's hit enter now we're inside my workspace as you can see right here and i obviously need a new larval project so that's right now larval new art app let's hit enter and this might take a second so pause the video and i'll see you back in a second the folder has been created so let's change directories inside our project so cd art app hit enter inside our auto app we need to add larval's ui through composer so let's perform composer require larval forward slash ui space double dash dev hit enter alright this was pretty fast for this course we obviously need to pull in the authentication scaffolding so let's perform php artisan ui view space double dash art hit enter and it's asking us to run npm install npm run dev so let's copy it paste it hit enter and this will probably run into an error of larval mix let's perform npm audit fix double dash force and now let's run npm run watch and as you can see it's compiling as well so we just fixed that now before i dive into my project i want to show you which versions i'm using for this project as you can see we run npm run watch so we need to open a new tab i can do that by pressing ctrl or command t let me zoom in cdw again cd into art app and in here let's perform php artisan space double dash version and as you can see i'm using larval's framework of 8.38.0 to be honest the versions from 8.0 until this one won't make a big difference the issue comes into play if you're using older versions such as laravel 5 or 6. now to also check out our npm version let's write out npm space v and as you can see the npm version is 6.14.11 whether you get experience in a different programming language than php or laravel you probably know that creating an application with login and register sessions password resets remember me access permissions can be well pretty time consuming and one of the reasons i started using larval is honestly to save myself some time when working on projects so right now it's finally time to dive into our code editor so let's open visual studio code and let's open our workspace as well i need to drag down my visual studio code alright so in our workspace let's drag odd app into the code editor let's close off the folder because we don't need it let's make it bigger and let's also zoom in let's close off the welcome screen now using a framework like laravel can also have its downsides and yes it does and it's even coming for me since everything is so easy to go and the documentation on larval is very good most people completely forget how the flow works and that's what i want to show you in this video every larval project comes with its own users table migration and user model which you probably have seen when you migrated your fresh project for the first time so let me show that to you if we open database there's a folder called migrations and let me make this a little bit bigger you can see that we have three migrations to create users table create password resets and create file jobs now the one that we need is the first one so to create users table let me make it smaller again and intellisense updated to well let's say dismiss all right let me scroll down right here you can see that a user has a unique id name email and an email verified at which is a timestamp every string so for the password the remember token and the timestamps now whenever you migrate this file it will be used with the user model so let's open that one right now let's open app models user.php looking at this file you usually won't touch it a lot you can see that the namespace is obviously app forward slash models it has some use statements as you can see right here it has a class name it has two use statements inside the class which i will cover in a bit and it has two properties the first one is called fillable and the second one is called hidden whenever you're using the fillable property you're basically telling the application well whenever you want to create a new user at least the name email and the password needs to be filled in and for the password and remember token exclude them because they don't need to be shown to the user and honestly that's right isn't it why would you want to get one of those values and print them out for a developer there's actually not a legitimate reason for it probably to see if your password is encrypted but that could also be done through my sequel there's a property called casts which will cast the emo verified at to the date time i skipped the most important word right here well actually the most important words so let's go at the top and as you can see our class user extends the authenticatable so i'm on a mac and i have php intellisense installed which allows me to click through in a word the way you do that is by clicking on alt and clicking on the word that you want to click through now in our case it's authenticatable so let's do that then here you can see the entire pad so it's in the vendor directory folder called laravel and so on until the file user.php you can see that the class extends the model which is the default model and it also implements three other files the first one is the authenticatable contract and this is something which is called a contract as the name implies this will require methods that will then allow the framework to authenticate instances of this model to the odd system we have the authorizable contract and if you have used laravel before you know that there's a can method which will allow the framework to authorize instances of this model or their access permissions in different contracts the last one is the can reset password contract which will basically allow you to reset password of any entity whenever you pull in the authentication scaffolding you also pull in some controllers so let's go at the top of our page open http controllers the odd folder now to show you this example or the controllers we need to go back to the terminal and in here perform php artisan serve and the reason why i'm doing this is so i can show you the files that we're working with now as you can see it generated an url for us so let's copy it let's open a new tab for the browser let's paste it in and let me zoom in a little bit all right now let's hop back to the cli one more time because i want to show you all the routes that we got and in order to do that we need to open a new tab again i need to zoom in and let's go to the right folder all right and in here let's perform php artisan route column list now let me zoom out for this one whenever you pull in the authentication scaffolding you will see a total of 14 routes right here and almost all of them are related to the authentication scaffolding now a cool thing is that the authentication scaffolding will automatically pull in a home controller so let me show that to you right here it has a home controller in the controllers folder and as you can see inside the uri we have a home page which will go to the home controller and it searches for a method called index besides that we have a login screen show the login form and something that might be weird for you is that we basically have two login forms so the get head and the post the get method is basically to show the page and the post method is to submit the form we have a logout page the password confirm the password email reset and the register page now one thing that you might have seen is that all these files that you can see right here go through the art folder so every single page right here is using the art folder and this is actually the reason why i showed you the routes since that's the first place we need to look so let's go back to visual studio code in here you can see that we have one two three four five six controllers that are related to authentication i want to go over these controllers in a logical order so whenever you start a new application you need to register first so let's open that one so the register controller what i want to do is to actually register an account and show you the different things that happen inside the controller but before we do that we need to set up our database so let's open our emv file let me close off the vendor i need to change my host to localhost the port is alright the databases are right and i am using a password of dari1234 save it and close off the file let's open the register controller again let's go to my sql now what we actually could do is to open a new tab and write down well zoom in write down my sql we need to create a new database so let's perform create database art app and hit enter and you can see that one row has been added now let's go back to the previous tab that we had in here we need to run php artisan migrate and let me zoom in as well right now we are ready to continue on with our controller i won't be going over the use statements namespaces and class because it's pretty straightforward it's something that you already need to know but the one thing that i skipped inside the model was the use statements now these look like a new statement but they are actually called different these use statements inside a controller class are called traits and this trade will be called when the controller is being called now if we scroll down you can see that we have a property right here called redirected to and this is actually pretty important to showcase what this does i want to go to the browser click on register inside the top right corner create a new account so let's say code with story password is info password is something secret alright if we click on register right here you can see that the register process went pretty fine but we have been redirected to forward slash home and this is being done inside the redirected to property of the controller as you can see it's calling the route service provider colin collin home what is common colon home if we press on alt and click on route service provider scroll down you can basically see a constant here called home and this is going to an endpoint of forward slash home and if we double check it inside the browser you can see that our endpoint is forward slash home obviously we can easily create a new endpoint so let's do that let's go right below our constant home create a public constant test and let's set it equal to forward slash test save it go back to our register controller and let's replace home with the content that we have called test let's save it let's go back to the browser let's click on our name log out and let's register one more time name is test password is test add code with diary let's add a password click on register and as you can see it redirected us to forward slash test and the status code is a 404 because the page has not been found now let's go back to visual studio code let's change it back to column colon home and let's continue on as you can see our controller is using a constructor so whenever a user tries to hit the register controller it will look if a person is logged in or not through the middleware there is a validator and a create method the validator method will basically define how to validate registrations so if you like to change some rules say that you want to change the password minimum characters to let's say 12 you can easily change it right here now the last method is the create method which will define how to create a new user based on the incoming registration which is the variable data whenever you register you will log in automatically just like i showed you a minute ago but let's see how the login controller works so let's open it if we scroll down you can see that the login controller is using one trait called authenticates users which will bring in the redirected users it also has its own property of redirect 2 which is hitting the home constant but you can change this one as well now there's one method inside the login controller and that's the constructor so whenever the login controller is being called it looks if the guest already exists so inside the session and then it will show you the log out button now i can go over the confirm password forgot password reset password and verification controller but if you scroll through it let's just open one you must have a clear understanding of what is going on right here because it's pretty much the same it's using crates it has a redirect and it has a constructor that might go through the middleware now the next step is the routes so if we scroll down and open the web.php file inside the routes scroll down you can see that three routes are defined but i actually want to focus on the odd column column routes now what this basically does is showing all the endpoints that i just showed you in the cli you can obviously define all the routes by yourself but think about it if you have a resource for crud applications why would there be a special type of route for all the different views that you have in the controller it's so easy to just define a resource or a route now that i've talked about the controllers model routes and migrations i think it's time to take it a step further by talking about the authentication scaffolding since views are a very big part of it it will take care of the routes inside the views of the resources folder there's a folder called views and in here you can see a odds folder all these files and the layouts folder as well got automatically created when we run the php artisan make odd command and all these files have a bootstrap based layout and all of the form fields are already correct now if we open the login blade file we obviously have a form where we can log in and i won't go over all of the stuff that we have right here but i want to quickly show you something which is pretty important if we scroll down to the bottom you can see that we have a label and the input right here this is basically the remember me checkbox that we have inside our view let me show that to you change the endpoint to forward slash login oh we're logged in let's log out let's click on login and as you can see right here we have a remember me checkbox now this is pretty much the place where we process the remember me checkbox that you can see in the ui this remember me function does not work out of the box since you need a long life access token inside your database so let's go to the cli let's go to the my sql tab and let's use autoapp now let's desk the table users and as you can see right here we have a remember token column which can be known now if we select everything from the table users let me zoom out alright you can see that the remember token off code with dowry and test are actually both new and this is happening because i've never clicked on the input field when you usually log in you log in without a remember token but whenever you click on the remember me checkbox a boolean will be passed as a true to the auth attempt method so where can we find this method let's go back to visual studio code let's go to the login controller and let's click through on authenticates user now if we scroll down you can see that we have a method or somewhere right here called attempt login you can see that we are going to check if a remember input field has been filled and this will return a boolean now if we go back to the browser let's log in with info at darinozar let's click on the remember me checkbox click on login you can see that we are indeed logged in and if we go back to the database select everything from users again you can see that a long life token has been created for code with dory if we close off the browser open it one more time let me make it full screen now let's go to the right endpoint you can see that we are indeed logged in because we cannot see the login and register button now the next thing that i want to talk about is the odd middleware and i actually have talked about it before but not really in depth a lot of places in our application let's open visual studio code and let's go to the login controller let's go to the constructor all right a lot of places in our application we have been using the middleware as you can see it accepts a string called guest now how does this work if you want to perform checks to see if a user is logged in or not you will usually do that through the middleware now laravel uses a route middleware where some very important rounds are defined and these can be found inside the app folder http and there's a file called kernel.php if we scroll down to the bottom you can see that we have a property called route middleware and if you look at the names if you look at the names you can see a couple files that are related to authentication the first one is obviously odd and this is used to restrict route access to authenticated users we have the odds.basic we have the guest right here which will restrict access to unauthenticated users and the last one is the can right above and this is used for authorizing user access to given routes now this was it for this video about how authentication works on laravel if you do like my content and you want to see more leave this video a thumbs up and if you're new to this channel please hit that subscribe button
Info
Channel: Code With Dary
Views: 14,998
Rating: undefined out of 5
Keywords: laravel, laravel 8, laravel php framework tutorial - full course for beginners 2020, laravel 8 tutorial for beginners, laravel php framework tutorial full course for beginners, learn laravel for beginners, learn laravel step by step, laravel tutorial youtube, how to learn laravel, laravel tutorial 2020 - the complete developer course, complete laravel 8 course, learn laravel 8, laravel authentication, laravel how does authentication works, laravel login register
Id: Gkh5W66FurA
Channel Id: undefined
Length: 21min 1sec (1261 seconds)
Published: Sun Apr 25 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.