How to create Custom Login Registration in Laravel

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we'll be seeing how to create a login and registration form in larval so you'll be able to add as much as Fields as you want in the registration part and you can see if you try to submit it without any values it will show an error message so we are also showing the error messages properly so we are not concentrating on the UI design so we'll be seeing the actual implementation of the larval code okay so this will be completely secure because we are using larval and we'll be also seeing the login part so you can see it is also showing the error messages and let's try logging in so you can see I have created a user earlier and let's try logging this user and submit it now you can see the user is logged in and on the top you can see home and logout and also the name of the user I will zoom in okay you can see this right okay and we are having a lockout button if you click logout it will get log out and it will redirect to the login page okay now that's all if you try to insert a wrong details and submit it you can see login details are not valid so we are also handling the errors we are not concentrating on the UI but we are implementing the program properly okay so we'll be seeing this in this video so let's get to the implementation part first of all we have to create a folder and open it inside an editor so you can see I have opened a folder inside PHP store you can use any other editors like vs code or anything so I'm using PHP store then we have to install larval to our system for that you can go to this larval.com website and click get started and here on the installation you can scroll down and you can see composer create project laravel laravel followed by the project name now this will create the project directly without installing the larval installer and the second option is composer Global require larval slash installer now this will install larval installer to your system so you can use a command laravel new to create new projects okay I already have created a video on how to install larval installer and all other required components like composer Sam Etc so you can check this video I will give the link for this video on the description you can check that out you can see we are installing samp vs code and composer so I will give the link for this on the description and also on the I button you can check that out okay now we have to create a new project you can either run this command to run this command you need just composer installed or you can install the larval installer I already have larva installer installed so let's run this command okay now we have opened the IDE now here we have to open the terminal so I am opening the terminal if you are using vs code you simply have to open the terminal from the menu options and you have to follow up what I am going to do here okay Now to create a new project you have to type larval new followed by the project name you have to remember that I already have laravel installer installed that's why I'm able to use this command okay otherwise you will get an error message so you have to install larval installer first okay I already have a video explaining this properly so you can check that out now we can run this command so here you can see larval has created the project so if you go to the folder on the top let's minimize the terminal first so if you click here you can see a new folder and it's having the name of our application then we are having many other folders okay so I will explain them so this is our project so in order to move to our project we have to use CD CD means change directory followed by our folder name that is custom underscore login now you can hit enter and you can see the directory has been changed on the terminal you can see this right okay now you can run this project and check how it looks for that you have to type PHP Artisan server so we have opened the server on this so you can click this link and it will open the project now this is the default way how larval project looks when you create it so we'll be seeing how to customize this and we'll be adding the login registration so we'll be doing the custom login and registration okay so for that you can minimize this and let's introduce you to the folders here you can see a folder app inside that we are having our HTTP and the controllers and you can see the models here we'll be seeing how to implement the database using the migrations okay we'll be seeing how to edit the table and run the migration also and we'll be checking the public folder and what it does so we'll be adding all our assets like the CSS file JavaScript images which can be accessed by all the users directly without any restriction can be added to the public folder and we'll be seeing the resources and Views inside this we will be adding our HTML and all those designing parts and we'll be seeing the routes inside that will be checking the web.php here we'll be specifying the routes that our application listens to okay so let's start creating so first of all we have to create a new view okay I will open all the folders once more let's we might solve the folders okay I have minimized all the folders so first of all we have to go to the resources and views now here we have to create files for login and registration okay so let's create one for login so here you can see I have named the file like login dot blade.php hope you understand the basics of laravel so here in the view section we'll be naming it on blade.php it is blade templating which is a feature provided by larval okay you can learn more about larval just by Googling it or you can check the documentation they have a detailed documentation I will also be creating a complete course on laravel so for now we'll be sticking to the login and registration part so let's create a file login.blade.php okay now we are having our file let's do one for registration now we have to create a file for layouts okay I will explain that inside this layouts we'll be adding boost app okay we'll be using bootstrap to design our UI so for that you have to go to get bootstrap.com you can simply search bootstrap on Google and get to this website I will give the link for all this on the description you can check that so you have to open this website and go to the doc section so in the documentation you can see Kickstart scroll down and you can see a starter template here so you can simply copy this much and you can paste that here so here we can see we have added boost up to our project so let's remove The Unwanted things like this okay now you can see this is the CSS part of the bootstrap and this is the JavaScript part okay so you can remove this hello world from here and let's change the title we'll be using the layout file as a layout for all these pages okay I will explain that when we do that so we'll be providing the title from the registration page or from the login page or even from the home page that we'll be creating later on so we'll be passing the title from these pages to this page so in larval we have something like at yield and here we can give a name okay so here we are receiving title okay so you can give title now this means that we have to pass the title from the page in which we are using this layout okay so I will show how we are going to use this so if we don't specify the title on the other Pages the title will be blank if you want to pass a default title if this title was not given on other Pages you can simply give a comma here and pass another parameter and you can give the title like this okay so hope you understand now inside this body we need to access the content okay here you can see we are accessing the content okay so here you don't have to specify the name like content or anything you can simply use something like body or something okay just have to make sure that you are using the exact name when you pass the value I will show how we pass the value okay I will show you how to extend this on another page so here you can see we are having a simple layout in Blade now let's go to login.play.php and let's extend this layout okay now you can see we are extending the layout file here so you can see I haven't used the dot plate.php extension here because blade will automatically render that and all so you don't have to worry about that so you just have to give the name of the file if you are having any folder like if you are keeping this inside some folder you will have to specify the folder like this is a folder folder instead of giving a slash we'll be giving dot okay then you can give the name of the file okay we don't have this folder so let's remove this okay okay now we have extended the layout file to the login so now we will see how to pass the title so here we are asking for title okay now what happens let's try running this and check for that for running this we have to add a route okay so go to the routes and go to web.php now here we can see one route and it is pointing to a view welcome if you check the welcome it is having some template that is designed by larval so let's close that and let's create a new route and in this route we are receiving get requests okay so these are the basics of larval so hope you understand the basics of larval so let's give login so our route will be slash login and for now let's pass a function callback function and inside that let's return a view and the view is login and let's close this here okay now this means that we are retaining a view that means this views folder and inside that we can see I am using locate that means this login.blade.php that's all if I am passing registration that means I'll be returning a view that is registration.plate.php okay so that's all let's go to the route and here we are returning this now we are having this route so let's copy this route slash login and let's open the terminal okay we are having the server running already if you want to run it again simply type PHP Artisan sir okay now click this link and you can give slash login here hit enter okay now you can see this page has opened you can see the title on the top if you check on the top section you can see custom oauth larval so this means that our layout is working if you go to the view Source section you can see the source that we have added on the layout section is there okay you can see the templates from boost app and all okay so let's go to the login now we have given the route for login let's design our login.blade.php I will not be explaining it much more because the video will get lengthier here I will stick on to the login and registration part inside laravel I will later on create a complete course explaining everything in laravel so let's design the login form for that you can open section section and we are passing a Content okay so our section name that we have given on the layout.play.php is content okay okay we are using yield content so here we have to give section content okay and we have to end the section like at end section okay that's all now inside this you can give the content that is the login form now we have to pass the title also in order to pass the title you can start section title and since we are passing a title we'll be only having a short line so we can give comma and pass the title login okay let's give the title login okay now if you go and refresh the page you can see the title will change from this to custom auth larval to login so let's go and refresh that page okay I have refreshed the page and you can see login here so now we have given the title now let's design the login form so for that we can go to boost app and inside the bootstrap documentation you can go to forms on the side you can see forms and you can select the overview you can see a form here so this will suit our use so let's copy this from here and you can paste it here before pasting you have to give a container div let's open it you and give a class container now inside this you can paste the form here okay now let's minimize the terminal and here you can see we are having the login form if you go and refresh the page you can see the login form there let's give a width for the login form so let's give a bit like Style and with let's keep it like 500 pixel okay now we can go to the login form and refresh the page okay here you can see the login form is appearing you can see the login form is not well arranged and all so let's add a navigation bar on the top for that you can go to the bootstrap itself so on the side you can see now bar okay see now bar so click that and you can copy one now bar that you likes to so let's scroll down and I think we can select this one okay so let's copy this and we can paste the now bar inside a header okay we have to create a header.php for that let's create a folder include for that and inside that we can create a file header Dot play.php and let's paste the okay I have made a mistake on the name let's rename this file you can go to the refractor and rename okay let's make it header okay now inside this you can paste the now bar okay now we have to include this file to the layout so come to the layout and inside the body you have to add include and then you have to specify the file name so this is Insider folder that is include so let's type include you can see the suggestions dot okay you have to use dot instead of Slash okay and you can see the file name header okay that's all now this is added to the project let's see and refresh the page so here you can see the now bar is present properly okay now we are having the login form and now bar let's make this form to a line on the sender so for that you can go to login.play.php and come to this form and create a class since we are using bootstrap you can use margin start that is m s and you can use Auto and you can use margin and Auto okay now let's refresh the page so you can see it is aligned to Sender you can also give a margin on the top okay empty Auto okay that's all okay now we have given the form so let's create a form for the registration part as well so let's copy this much and go to registration.play.php and paste it here now you can see the title we can change it to registration okay now we have again the title now we need name email and password for registration right we may not be needing anything else if you need anything else you can add it okay so I'll be using name email and password okay so for that you can copy this much and you can paste it here and let's change this to name and this part you can either change the ID or you can simply remove it for now okay let's remove it okay let's remove this ID because we will not be using this ID so you can remove it okay now you can change this from here remove that okay here you can remove this also okay so here we have to give the name for the input tag since we are receiving a full name we can give something like full name or name okay so this name depends on the table so we have to create a table we will get to that later on okay so let's give the name that is name okay and you can remove all these things from here okay let's remove this and all and here you can give the name email okay and same for the password you can remove the ID and we can remove this check to remember this thing because we will not be implementing this right now so we can remove this and yes you can give the name for the password okay we have given the name for the password okay so our registration form is ready okay so now we have to go to the login and do the same let's copy it from here because we have already done the part here so let's change the title to login okay now we don't need the full name so remove that okay done that's all so let's add the route for registration go to web.php inside the routes and you have to add the route for registration as well so simply go here paste it and you have to change this to registration okay that's all now you have to change the view also inside the view we are having registration.blade.php okay so let's give you the view name that is registration so we are returning registration okay so let's go to the web page and try accessing the registration route so we are inside login right now so let's change log into registration and hit enter okay you can see the registration form has opened so we are having full name email and password so let's try implementing it so now we are having a registration form and login form ready let's go to database folder and inside that you can see migrations and open that you can see the table migration for table uses okay so this is the table name uses so this is one migration that larval creates by default so either you can change it or you can create a new or that doesn't matter so we are going to stick to this because we need a user and for the user table we need ID name email you can also add email verified diet that doesn't matter because we will not be implementing the verification part in this video because on the later videos I will show you how to do that okay you can add that as well don't need to change that and we need a password and you need to add remember tokens because if you want to implement that you can Implement that later on you will not be seeing how to do that right now okay and we need timestamps inside this timestamp we'll be having if you want to open this you have to click control and click on the mouse here okay so you can see we are creating created at which is a timestamp and updated it which is also another timestamp so this stores the times where we create the row so update the row Etc that's all so let's run this migration okay so we only need this we don't have to change this migration so if you don't understand the migration part of larval you have to check a video I haven't done any scores on that right now I'll be doing it later on so you can check that out that's all it's very simple this is the way you create a table and this is one you have to this line you have to add it because it drops the table if it already exists that's all okay so let's try to migrate this table so for that you have to go to dot EnV file okay you can see this here and here you can see the database connection okay DB connection and we are using MySQL so let's create a database okay so let's name the database custom login itself so for that you have to open Sam now I have opened the Sam I have installed samp and let's start the Apache and MySQL and you can go to the admin part of MySQL now this will open the phpmyadmin okay now we have to create a new database click on the new and you have to give the name of the database here so we are going to name it custom login okay this is the name of our database okay so click create and that's all we don't have to do anything else on the PHP my admin panel okay now you can go back to the PHP store if you are using vs code you simply have to go back to your vs code and open the terminal just like I do in the PHP stream you can also open the terminal inside the vs code okay so open the terminal and you can see I am going to open a new terminal inside vs code also we are having the same option a little bit different UI that's all you have to keep this running you should not stop this because we need the server running in order to access the project so let's create a new one new terminal I have opened the new terminal and we have to run the migrations you can see the username and password you have to remember this okay by default Sam is having a username root and password that is empty okay that's why I'm not changing this if you have a different username and password you have to change the username and password so you have to make sure the credentials here are correct okay okay now let's do the migration so we are going to create these tables okay these tables will be created you don't have to worry about these two you can check that later on when you learn about larval in detail or if you already know level you'll be understanding what this is okay so let's run the migration for that you have to use the command PHP Artisan migrate okay you can see that an error has came you can see cannot open the file or design that's because we are not inside the project folder we are inside this folder and we are having one more folder that is custom login and inside that we are having the file Artisan okay so you have to change the directory so CD custom login hit enter you can see we are on the custom login folder now we can run the PHP Artisan migrate okay now you can see the migration is complete so let's go to the PHP my admin and check the tables let's refresh this page this is our new database that is custom login let's refresh it okay you can see new tables has been created we will only be checking the user table we can check all other tables later on these are some things that lar will use for this is for password reset and all so we'll not be checking that right now so let's go to the users table okay and go to the structure so we need name email and password while registering okay the ID is auto increment and it is a primary key and email is unique okay unique that means we cannot insert similar emails that's all so let's go to the PHP store let's minimize the terminal and you can close the EnV file we can come back to this later and you can close the migration files as well okay now go to resources views and we are having the login and registration forms now we have to create a controller for managing the login and registration so for that we can create One controller let's name it auth manager so open the terminal once more and you have to use the command PHP artisan make colon controller space you have to give the name of the controller okay now this is the command and you can hit enter now you can see it says the control has been created on this folder so let's go to that folder minimize the terminal and inside the app HTTP you can see controllers okay here you can see one new file has been created that is oauthmanager.php open this you can see it extends controller okay so here we have to create our function for login and registration so first of all let's do the part where we want to show the login form and the registration form for that function let's name it login itself login here we'll be returning a view that is will be simply changing this from here to this controller okay since this is not an ideal way to code so we have to change it from here to our controller that's all this is what we are going to do right now okay create a function for registration same we are going to change the view returning of the view from here to here okay now we have to call these functions right because right now these functions are not linked to anything we just created them okay so in order to call these functions you have to go to web.php that is inside the routes inside routes you can see web.php and you can remove this function okay till here okay this much okay then you can call the controller that is oauth manager inside an array okay oauth manager and you have to call it like this okay and here you can see it is accessing it from here you can come to here and click here and type Alt Enter okay this isn't shortcut inside PHP storm you simply can remove it from here and you can add this like use oauth manager okay you can see the suggestion okay you have to Simply add it like this if you are using PHP storm you can simply type alt and that will go to there so it is actually easy to use PHP Stone I don't know how the suggestions work inside vs code okay now that's all you have to add this because here we are accessing this this auth manager so we have to add this line okay and you can call the auth manager like this okay that's very simple and our array inside our array we have to pass the function as well this is our class and here we are going to pass the function name so for login we are having the function name that is logging okay now let's copy it from here and let's paste it here okay now you have to close this okay and change the function name to registration okay now we can give the name for all this route okay you can give a name for the route that is like this name so for login we are going to give a name login that's all and same for the registration okay so now you can call these routes using these names if you are having a different name you can simply call by that name we are using login and registration itself in order to avoid any complications so we might not forget about that okay so we are having these routes ready okay now we have to create a post route for the login because here we are only having a get request running and that's on the slash login so we need to create a post request which will actually do the login path okay this one simply displays the login form we haven't done anything that relates to the database and all so in order to handle that we have to create a post request to the login okay here you can see we have created route to the same route that is but we are having a post request okay so you can see these two are actually different okay don't get confused I hope you understand the basics of larval so we have to call it different function here okay and you have to give a different name Also let's give a name dot post okay now let's give a function name like login post now we have to create this function go to the auth manager and come down here create the function now inside this function we'll be receiving a request which contains all the data that is passed from the form so for that you can give a request you have to make sure that this request is from the illuminate HTTP request okay this is very important otherwise it will show error space you have to give variable that's all now you can access all the data that is passed from the login form to this variable for that you have to add a action for the form okay so let's add an action action and here we have to specify the route so in order to specify the route you can simply do it like this and give the route and here you can give the name of the route so here we are having the name that is login so for this we are giving the name here okay in order to access the routes like this we are getting the name okay hope you understand that also now we have to specify the method and the method is post so we are making a post request which passes the email and password okay to this route okay to this route that is the post route and it will call this function so here we'll be getting all the data that is passed from this form that is email name email and password that is the name password okay so we can access these password and email like dollar request of password and this will give you the password that you entered here okay that's all let's do the login part so first of all we have to validate this request so for that dollar request variable and we have to validate call the function validate okay not this one and here we have to check for email we are receiving email give it a required so this means that we are having an email and if this is not present we will return an error message okay now we can do the same for password you have to give a comma here okay you can give a semicolon at the end also now change this to password if you don't know what these names are we can see in this login form we are giving name right you have to use this same name here okay hope you understand that okay now we are checking if the values are present in this line we will return an error message automatically larva will return an error message saying that email ID is required Etc so you don't have to worry about that so this will check if the email and password is present okay if this is not present it will return an error message so now we have to to the login path for that let's create a variable credentials equal to dollar request of only so inside this function you have to pass email coma password okay that's all now you can hit enter and we have to try to login for that we are having a class so inside if we can call the class that is oauth auth and you have to make sure that this is inside the illuminate support and Factory okay you have to call the attempt function okay now inside this you can pass the variable that is dollar credentials now this will do the login automatically you don't have to worry about anything so you can open this if condition now this will return true if the login is successful so if this is true we have to login the user to home page and we can also show a message that is login successful or anything like that so for that return and we have to redirect the user so for that redirect okay now intended inside that we have to give the route to which the user should be redirected to so right now I haven't created a new route so let's give a semicolon and go and create the route first so go to the web.php route section and we have to give the name for this route okay I'm going to change this later on so let's give a name here name hope let's give an info okay and you can use that name here inside this okay so for that route you can have to use route call the function route inside that give the name that is oh okay and if this is false you can return some error message so let's return an error message for that tone redirect and if this is error we have to redirect the user to login page right so route so we are redirecting the user to login and we have to pass an error message also so give something like this and you have to give the function that is with and here we have to pass a key and let's give the key like error okay so you can decide the name but you have to make sure that you use it properly okay I'm using error and you can pass the error message now this is the key this is the key and and the second one is message okay so let's give something like login details are not valid or something okay so give an error message that's used for you now that's it now you can also give a success message here but we can avoid it if you want to give a success message you can simply copy this from here and you can give it here and instead of error you can use some things like success okay that's all and you have to print this message by yourself since we are not going to do that you can remove that because we are not going to show the success message on the home page because we will only redirect the user to home page if the login is successful so that's not need to show the user as access message and all so let's remove that okay now this is our login part and it is completely done okay this is 100 secure and you can trust travel for that now we can get to the registration part for that we have to do the same that is create a post request for the registration so you can copy this line you can copy this line and you can paste it here change the route from login to registration and same you have to change this to registration post this is the function name okay don't forget that we are simply copy pasting because we have to do it faster in order to make the video smaller as possible okay and you can gave the name here that is registration dot post okay this is the name of the route okay you can access this route using this name okay you can give any name but you have to remember that that's all now you can go to the registration blade that is the play dot PHP part for the registration and you have to do the same things that we have done in the login form you can copy this much from here okay and you can use it here okay now you have to change the route here so our route will be registration dot post I think okay the name of the route is registration.post okay let's copy it from here name from here and go to the name yeah you have to change it to here okay so this will print the route that is the post route that is this one okay you can check by viewing the source code after running so you can see the route and all and you will understand what it means okay okay now this route will only execute the post okay so we have done the post part now I remember that inside the login we are accessing the route that is this one okay and this one is for displaying the four that's all this is a get request we have to use a post request so this is the name so you have to make sure that you didn't get make stand do it wrongly you have to give the name okay login dot post okay be sure that you give the name of the route properly okay now for running each form larval have a security feature that is you have to add a token okay lar will verifies this token so this avoids a request from another sites okay so this will validate that the request will be made from your own website so in order to add that security feature you would simply have to add at csrf that's all now when you run this you can see this line will be changed into an input tag that is token and it will also have a value that is the token value okay and larval will do the verification of that token you don't have to worry about that simply add this that's all if you don't add this you will not be able to execute this form so remember to add this okay you have to add that on the registration also okay now let's do the registration part in the registration part we are having the name email and password you have to make sure that the names that you use are proper okay because we will be inserting it directly using these names I will show you uh you can go to the auth manager and let's create a function okay so you can go to the web.php route and you can see we are having we are calling the function that is registration post okay and we are not having that function so let's create that function function registration post and yes we are receiving the request so for that request dollar request okay let's open the function now you can do the registration part so first of all we have to validate the request you can copy the validation part from here and paste it here and you have to remember that we are having one more field that is the name so simply add name on the top that's all you can also add more verification to the email tag like you can add this is an over simple email now this will check if the email is an email like it will check whether it is having an at.com dot in ETC some steps like that okay and you can give another condition that is unique user that means the email should be unique for each user so unique Cola users so that's all so now our verification part is done and you can go down and you have to create the row insert the row to the table for that let's create a variable dollar data and let's access name so we are going to assign the name from the request dollar request you can access the name like request and you have to give the arrow then name okay now this will access the name from the request and okay you have to assign it you have to give an equal to sign okay this will assign the name from the request variable to this variable that's all okay now we can do the same for email and password okay so we have assigned the values now you can create the row that is inside the user like you have to receive the data like use dollar user equal to user this is the model that is this model this is already created here okay we haven't created this model but this is already created okay so sometimes this will not work okay it will show some error like the table name doesn't match Etc so if you face some error like that you have to come here and add a variable that is tables okay protected table okay protected table equal to and you have to give the exact name of the table so our name is like users so this is a model that can do all the database queries and all the interactions to the database for the table users okay that's all a model simply does all the queries and all the interactions with the database and it is already done and kept and we don't have to write the function again and again you can simply call the function and that's it so it makes our work easy and fast so you can specify the table name like this okay in order to avoid any error that could happen okay and that's it and user you can code the function that is create now you might not get any suggestion because some of the functions on larval will not get much suggestions okay and inside this you have to pass the variable that is data now here we are going to pass an array okay so let's pass data now this will create the user that's all you simply have to pass the values like this now we have to encrypt the password so don't forget to do that so for that you have to use class hash h a s h and you have to make sure that it is from the illuminate support and far grids and inside that you have to access the function make okay okay now you can see this is in yellow that means we haven't added the Top Line this one okay use this line you have to add that so inside the PHP storm you can simply click here and type altender and you can click import class and it will automatically add the line okay you can see the light if you don't have this feature you can simply type this line okay just make sure that you are using this hash function inside the illuminate support factors and the class name okay so you must make sure this is exactly the correct okay now here we have encrypted the password and we have created the user that is we have inserted the user to the table now you can check if the creation of user is successful or not and show an error message or redirect the user to the home page for that you can type if not dollar user that means if there is no user you will be executing this statement that is a return and we are going to redirect the user to the registration page with an error message okay so for that let's copy the error from here okay since we don't have to type it again so this is the same statement so we have copied it from there and pasted here and we have to redirect the user to registration not login so change the route name to registration hope you understand the concept of this name you have to check the name that you have given here okay if you understand that part okay now here we are passing with error this is the key name and this is the message so let's change the message to something like registration failed try again something like that okay okay now we have given the error message also so after the if condition you can redirect the user to login page so we have done the registration here now after successful registration we'll be redirecting the user to login so they can login and access the app okay so for that return so that also you can copy the line from here because we are going to redirect the user cell so and paste it here and yes we are going to redirect the user to login and that is correct and you have to pass a success message not an error message so let's change the key name to success and you can set a success message like registration successful and with a message for logging in let's try something like okay so I have set the success message like this now if the user is not created and something happens with the registration it will show an error message inside the registration itself okay we will not go to the login it will show the error message inside the registration page itself and if the registration is successful we will redirect the user to login and show a success message okay now we have done with the registration part as well so that's all with the registration part we can also do the logout part as well that is very simple you can create a function log out and inside this function you simply have to clean the session and call a function inside the auth that is logout so I will show you after this you have to call auth log out that's all and the logout is completely done now you have to redirect the user to a login page okay so for that you can copy this much from here and paste it here and give a semicolon you don't need to pass any message because we simply are logging out a user okay so that's all our logout part is also done but we have to give a route for the logout so go to web.php inside the routes hope you understand the location of all this where the routes are present and where the views are present inside the resources you can see views and were the model is there you can see inside the app folder inside that you can see the model and inside the app folder you can see HTTP and inside that you can see controllers hope you understand the folders how the folders are kept inside the larval okay now you can go to the web.php route and you can add the route for logout so let's go to the bottom last line and create a route route we are using a get request and you can give slash logout and here we are accessing the oauth manager class that is oauth manager and we are calling the function logout we already have created the function okay now you can also give a name for this route we are always giving name for the routes so we can access it from universe log out and give a semicolon that's all so all our routes are ready and you can also change this from here if you want to let's keep it here no problem because we will not be doing anything on the home section we will not be designing the home we will simply have to return the view that's all we'll change the view later on okay okay now our logout is also complete now we can test all this okay for that you can open the browser so I have opened the registration page inside the browser okay let's fill some details on the registration and try to register user I have inserted the details full name email and password now let's click on submit okay you can see the full name is showing that it is a email so let's go to the blade part of the registration and here you can see the type for the name that is email so change it to text and that's all that was the error okay now I have again inserted the values now let's try submitting it okay you can see the registration was successful and we are redirected to the login part since we are not displaying the messages the success and error messages we have to display the success and error messages by ourselves okay and we are not doing that on the part that is the blade part we will do that so you can see the registration is successful okay let's go to the phpmyadmin and check the table and you can see the user right we have inserted the name email and password that's all password is encrypted okay that's it so the registration is successful now let's show the error messages and the success messages for that open the PHP store so you can see I am on the registration blade part okay we are having the form here so before the form out here on the container we can create a div and give a class name and let's add a margin to the top margin top five okay now inside this we can show the error messages and the success messages and you can create a better UI I am not concentrating on the UI part you can design a UI properly so this is not the UI part that I am showing I'm showing the larval registration part okay now you can check the error messages like at if and inside that you can check dollar errors okay and you can check if any errors are present for that you can access the function that is any now this will check if there are any errors okay if there is any error it will come inside this function and let's close the condition that is at end if now this is the Syntax for the blade okay so this is a bit different that is you simply have to use an at that's all you can check the larval documentation to understand more about that it's very simple you simply have to add an at and you can access PHP like this okay now inside this we have to create a div foreign call it class give a class like coal 12. this is bootstrap you can check the bootstrap to understand that so this is column with the maximum width that's all the inside the arrows variable we are having more than one errors so we have to Loop through the errors and print them so for that at four each now we are having dollar errors all okay we are getting all the errors this is an array okay as taller error and you can end the forage like at end forage okay okay now inside the forage you can print the error so for that you can create a div and you can give a class that is alert so this is bootstrap if you don't understand this you can simply follow along and you can see what it does and alert you can use alert danger now this will give a red color that's all okay now inside that you can print the message that is dollar error so here we have displayed all the error messages and we haven't done the displaying part of these are messages okay these are the custom error messages that we pass so for that we have to do something like this these are automatic ones okay these are the ones that larval passes and we are going to print that that's all so the ones that we create are this one okay with and with the key so these ones okay this access message and error message Etc so in order to display this we have to write another one that is at if so these are sessions okay so we are going to check session and we are going to check has okay has session dot has arrows okay not a dollar it's simply error okay so this is the key that we are using here okay now here we are checking if the session is having something with the key error okay now we can end the condition okay inside this you can format the code inside PHP stream you can simply click Ctrl alt l that's all so inside vs code it will be having a different way of formatting the code so you can check that online okay now inside this we will be printing the error message that's all so for that you can copy this line from here and you can change this variable and you have to print the session that is session inside that you have to give the key that is error okay that's all now here we are printing the error message now we can print the success message just like this so we can copy this from here and paste it here format the code yes now you have to change the hash error to has success so we are checking the key success and here we are going to print the key success and change the alert danger to alert success okay these are the bootstrap classes you have to check bootstrap for that okay now that's it so we are printing all the error messages here so you can copy this from here this much the margin top and this much copy this much from here and you can paste that on login as well because we need to show the error message on login as well right okay now you can paste it here and that's all so this will show all the error messages on login and registration so let's try running it okay now here we are inside the login form so let's try submitting it without doing anything without inserting any value we can see the error messages these are the error messages that larval sent because we are using the validate function okay so hope you understand that so if you want to know which part of the line is being executed for these error messages I will show you on the code you can see this much line okay this line this much like I don't I think it is four or five lines so this line is getting executed okay and inside this we are having two errors that's why we are getting a two alert messages okay so hope you understand that these ones are not getting executed okay this is the one that is showing the error messages right now okay so let's go to the browser okay you can see their messages now let's insert some dummy value you can see I'm already having a value let's submit this okay you can see login details are not valid now this is the session message that we have passed the custom okay custom session message that we have passed from the oauth manager controller okay so we are that is also working now let's try logging in with actual details okay so we have already created a user with the email support at codcc.com okay I hope you remember that and let's try logging in that user okay let's submit it okay now you can see that we have successfully logged in and it is opening the home page now we have to redesign the home page let's read the resend that and add a log out button and okay so go to the PHP stream and you can go to the welcome.blade.php inside the views folder okay let's remove everything from here Ctrl a backspace and we have to extend our layout that is at extend and you have to give the name of the layout that is layout that's all and now we can give a title okay for that at section and the key name is Titan so give the key name and now pass the title okay let's give the title like a home page you can give any title okay now we have to start the session for content since the content is much longer than one line you have to end this section like this at end section okay now inside this you can actually print anything okay so we'll be printing the user so let's try printing the user logged in user okay we'll be printing the logged in user for that you can use a function a u t h oath iPhone you can call user okay now this will print the logged in user okay let's go and try refreshing let's try refreshing this page now you can see since we are logged in we can see the details of the user ID name email and all those things this is the created at updated at dates okay so you can see all those details as a Json format okay I hope you understand that you can access all the user details like this now if you want to access the name of the user so you can do like add a hyphen here and you can give name now this will only print the name that's all and you can see we are having the name here let's try refreshing it so see it is only printing the name I will Zoom it for you you can see the name right let's zoom out okay hope you can see it properly okay now we have to change the navigation bar okay you can give the name of the app here and let's give the login and log out registration links here in this part okay for that you can go to the include header.play.php and you can see this line here right you have to remove that and you can call config okay since we are using the blade you have to use these brackets and inside that you can call config this is a function and inside that we can pass app dot name now I will explain this now this config function will point you to this config folder okay and this app will point you to this app.php file and inside that you can see name right and it is pointing you to environment and with the name app name I will show you that so here this will access the thing that is this variable inside this okay this is a configuration for the you can access this name like this from the configuration like this okay I will show what is actually the value so for that you can go to the environment okay so if you are not having any value inside the N1 variable this variable it will automatically show a default value that is this you can change this value as your wish okay now go to the dot tnv file and you can see apnea right so you can change the name here so if you are having any space anode you can use double quotes Okay like this and inside that you can give the name okay I have given the name for the app now you can go to the header and we will refresh the page later let's do all the other parts so now we have to show the login registration and logout links so for that go to here space and login and we have to show the registration to access the login link you have to come to this href and change this to call the route okay route and give the name of the route simple that's all plug it we have named the route hope you remember the naming okay now you can access the registration just like this route registration okay that's all so we are having the login and registration linked properly now you can copy this and you can paste it here and change the name to logout and change this text to logout okay now let's go and refresh the page so here you can see an error message it is saying that there's no property name and you can see it is showing the error to this line right you remember the home page so let's go to PHP store and you can come to the welcome.play.php and remove this because we cannot access it simply like this if we have to check if the value is present or not right now I think we have logged out from the app so that's why it is showing something like that okay we can remove it from here and let's try running it okay let's refresh this page you can see the login registration and logout links are working right and also you can see the name of the app right okay so let's try logging in once more so I have inserted the details okay this is the user details that have created earlier hope you remember that okay now let's submit it now you can see the login is successful so let's try printing the username once more for that come to welcome.play.php and you can print the name here okay so for printing the name okay so now we are printing the name let's try refreshing the page now you can see the name is present properly now if you try to log out and try to access this page you will see the error message that you saw before so let's try logging out now we have logged out from the page right now we can try accessing the home page for that just remove this login and try hitting enter see now you can see the error message the same error message because we are not logged in right now and we are trying to access the values that will only be getting set when the user is logged in okay this will only work when the user is logged in okay this auth user will store the details of the user that is currently logged in hope you understand that so for that you can actually do something like this so at oath can use at auth and you can use at end oath this will check if the user is authorized then it will only show this name otherwise it will not execute this line okay now let's try running this so you can refresh this page right now and you can see it is working without any problem right let's try logging in once more okay I have entered the details and let's try submitting it now you can see the name is printing properly right so we are logged in and now we can see the name so if we are clicking the logout the name will not be present that's all okay now we can also do one thing that is if we are logged in we don't have to show these two links right for that you can do the same technique that have done in the home page on the navigation part that is the header.they.php part okay so for that go to header.play.php and you can come here so here you can type at oath and if the user you can remove this practice okay if the user is authorized that is if the user is logged in you can show the logout link okay so let's copy it from here cut it from here and paste it here okay and you can also type at else else or okay and at the end you can type at end author okay let's format the code yes that's all now if the user is logged in it will only show the logout link okay and if the user is not logged in we will show the login and registration links okay okay now let's try refreshing the page you can see the login and registration page is gone and we are only having one that is logout okay we can change the name from here and we can show it here okay for that you can come to welcome.play.php and you can copy this much cut this from here okay and you can paste it here that's all you can format the code okay let's change it to one single line because okay like that okay now you can see we are using lsoth okay we can change this to else now you can see if the user is authorized it will show this and if it is not it will show this okay now this is the way to write it okay should not use else okay now let's try refreshing the page and you can see login and registration and that means that the user is not logged in okay let's try logging it okay we can see that I have logged in so you can see the name here and the logout option here okay that's it now hope you understand this much so now if you try to access the login page once more it will open it let's try it see now it is opening the login page again and that should not happen right so for that you can go to the controller oauth manager and you can come to here inside the login here you can check if oauth of check okay and if the user is logged in we have to return the user to the home page so for that you can redirect the user you can copy this link and yes paste it here and the route name is home okay and we are not having any messages so we can remove this okay that's all okay now this will check if the user is logged in you we cannot access the login page so you can do the same for registration as well okay now let's try refreshing the page so let's refresh this page see we are automatically redirect to the home page see now if you try to access the login page again you can see we are getting redirected right so let's try accessing registration see we are again getting redirected so let's try logging out see now we can access token and registration see that's all so we have done the login and registration with larva okay so hope you understand now that's all with the login and registration part in larval now if you want to create a new route let's say for profile so you can do something like this okay so you can create a route route let's create a get slash profile and for now let's return a function let's return something like high or something now you can see that we are having a route slash profile and we should only be able to access this slash profile when the user is logged in otherwise we should not access the slash profile route so let's try accessing it right now so inside the browser you can see let's type slash profile okay now you can see the high message right and right now we are not logged in you can see that we are not logged in see and we can access the slash profile page right so that is not good so we can remove it like go to the PHP storm and you can do something like this okay create a route group and you can mention a middleware now here we are actually checking if the user is logged in or not that's what this middleware is doing okay and you can give a comma and pass a function now what you have to do is you have to cut this route from here and paste it inside this route group okay so whenever you need a route that should be accessed by someone who is only logged in you can do the route inside this route group okay so that's all so you can do whatever things that a logged in user can access inside this route group and that's all so hope you understand that part also so let's try accessing it once more okay right now we are not logged in okay you can see that okay now let's type slash profile okay let's see tender and now you can see that we got redirected to login see let's try accessing it from registration page so you can see that see I am going to hit enter see now we are already directed to the login page okay now hope you understand so if you are logged in we can access the slash profit I will show you that also okay now let's login see now we can access the profile page you can see that okay okay now you can access the home page that's all you can also access the profile page okay now you can log out so that's all so that's all with the login and registration part in larval if you want the full source code for this project you can come to this URL I will give the link on the description you can check that out and you can download the source code from here so hope you understand this so if you like this video give it a thumbs up and subscribe to this channel for more such videos
Info
Channel: Codes Easy
Views: 91,708
Rating: undefined out of 5
Keywords: codeseasy, laravel custom login and registration, laravel login and registration, laravel custom authentication, laravel auth, laravel custom auth, laravel custom auth login and registration, laravel custom login register, laravel authentication, laravel 9 login and registration, laravel 9 authentication, laravel custom registration and login, laravel custom login authentication, laravel login logout, laravel 9, laravel tutorial, laravel login, laravel register
Id: jmTJBGxn8vA
Channel Id: undefined
Length: 81min 57sec (4917 seconds)
Published: Mon Dec 05 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.