Advance CodeIgniter 4 Framework Tutorials #6 Multiple Authentication Role Wise Login in CodeIgniter

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys welcome to our android tutor i am sanjay we are learning advanced codeigniter for tutorials this is our part six inside this video we'll see the concept of rule-based login inside codeigniter 4. so first of all to implement this concept we need to understand that what is a rule-based login suppose we have an application inside that application we have multiple user roles like we have an admin editor blogger author and we want to give access to all those users to login inside same application with different different credentials obviously their user roles are different it means they have their own dashboard according to their privileges so this is termed as role based login inside same application we are logging with different different user roles now inside this video we will see the concept of rule based login so for this video we have an article inside blogging website so if i back to browser go here click on blog or simply click on article codeigniter foo and search for multi-auth press enter now inside the search of multi-auth we should see an article something called code igniter for multi-auth user role wise login so if i click on this article this article is totally from scratch up to application installation up to its testing so we will follow each steps to see that how can we implement code igniter for multiple authentication inside first first few steps we have like installation of code igniter for and database connectivity inside last videos this is the setup what we have created and inside this setup we have configured all about this environment variable file as well as database connected and also we have created this database and inside this database right now we have few tables so what i will do actually we are starting this tutorial very from scratch so i will delete all these existing tables first so now we have an application and also we have a database and this database is connected with this application back to article scroll down so here we have all about setting the environment variable what already we had done and here we have create database and table so we have a database and inside that database we need to create a table called tbl underscore users so i will copy this mysql command back here go to sql tab pasting it here and if i press ctrl enter or click on go button it will create a table with the name called tbl underscore users so if you go to a structure here we have the table so if we click on that table go to structure so inside this table we have few columns like name email phone number here we have our role column which is enumeration type and inside this rule we have specified that when we do any entry it will be either an admin or editor right now we have two user rows according to your need you can specify multiple user roles inside this role column here we have the password and created add so successfully we have created this table now next we need to connect already we have done after creating table now we need a data seeder what is the role of a seeder inside codeigniter 4 simply seeders are used to seed dummy data inside any specific table so right now we are creating a user seeder which will be used so to seed dummy data inside our users table so i will copy this command battery terminal paste it here pressing enter so now we have created user seeder file if i back to editor go and reload to find that created file go inside this app folder database folder seeds folder and here we have user seeder if i back to blog article so here is the complete code of user seeder.php file so i will copy go here remove this existing code and paste in this new updated code inside this code what actually we are doing simply here we have the name space this is all about our parent cedar class here we have loaded our model so we need to create the model as well back to blog article scroll down [Music] so here we have set application model so i will copy this command back to terminal paste it here pressing enter and user mode model already exists in our setup so what we do next simply i will copy the whole code of this user model clicking it here back to editor go inside this setup all the models file we will find inside this models directory here we have the user model so i will remove the existing code and paste in this new updated code inside this code here we have specified the table name we have the table as tbl underscore users and here we have all the columns what we have inside this database table back to article and before that if we go inside this user seeder now we have loaded our model what we have created inside this user seeder we have a run method when we execute this cdr file to see dummy data via terminal command so spark command always look first this run method inside this run method we are simply simply creating an instance of user model and by the help of this insert batch method we are inserting two rows of users this is first user second user look at the rule this is an admin and this is an editor so we are inserting two dummy users inside our users table with different different user role go here all we have done with the sitter file this is all about sitter file now we need to see this dummy data inside our table so right now inside this table we don't have any data so to seed any setup file this is our command we need to specify only about the seeder class name so php spark db seed in this case we want to seed our user reader file so that's why we have a specified user seeder pressing enter data seeded successfully if you back to table click on browse and as we can see that inside this table we have two users with these dummy details and look at the role this is an admin and a theta if we want more than one user rules simply we can specify here inside this structure inside this rule column and we can work now the next step we have called routes configuration so before ours configuration we want user controller so what i will do simply i will copy all these routes go here open up the file routes.php from this config directory here is the file and inside this file i have pasted all these routes let's understand what these routes means so first we have a login route this login route will hit by using get request type and post request type and this login route is basically calling the login method from user controller and also here we have applied a filter called no earth so why we are calling to request i for the same method called gate and post because by using get request type we will display a layout that's a login form but after submitting login form with a credential we will hit the same method called login but this time we will proceed with post request type next we have the admin route here as we can see we have admin as a group and when we type localhost admin that it will call admin controller and index method now we have editor routes when we log in via editor user role and type editor into url it will call a data control and index method these two groups the first group is all about admin routes and the second group is all about editor routes we can specify more than one route inside this block right now we will only see all about welcome page and finally here we have the logout method means the logout route so from these routes we need a user controller admin controller editor controller and all these respective methods like inside the user controller we need login and logout method inside this admin controller we need index method inside this editor controller we need index method so apart from controllers also we need two filters called no auth and auth so after writing all these routes if i back to blog article we have set application model now we need to configure our filters we need two filters auth and no auth so inside this first step we are setting all about our author filter so copy this command back to terminal pasting it here press enter so auth filter created successfully and this auth filter will be stored inside this filters folder and auth dot php file so if i back to directory close all these files first we need to go inside this app directory filters folder and inside this filters folder we can see we have auth.php this is the by default generated file so what we want simply i will copy all this code from this here go inside replace with this new updated code so inside this earth filter what actually we are doing inside this before method simply we are checking that if currently we are logged in or not we are checking via is logged in method variable and here we are checking that is logged in if it is set if it has some value then it's okay otherwise if it has no value here we have not symbol this is logical not if is logged in is empty or it is not accessed it it has no value so in that case we need to redirect on login route so by the help of these three lines of code simply we are checking that if we have a valid login or not if this variable does not exist then we need to go inside this login route that's all for this auth filter back to blog article now next we need to create a no auth filter so i will copy this command back to terminal execute that now it has created no auth filter so go here as you can see we have a new file no auth i will copy the complete code from here go and replace with that and simply inside this new auth what we are doing inside this before method simply here we are checking is logged in again this time if is logged in exist if it has some value then we will go inside this block but inside this auth filter if this variable does not exist and it has no value then we need to go inside this block so let's say that is locked in exist and it has any kind of value so go inside this block of no what filter inside this block we are checking that the user role also we will store a session variable called rule inside session so after login we are checking that inside session if we are variable with the name called rule and inside that role if we have value equals to admin then we need to go inside this route otherwise if rule equals to a detail so in this case we need to go inside this editor route so simply by using this no earth filter we are checking that the logged in user if it is equals to admin go inside this route if it is an editor go inside this route that's all back to block article now next we need to register our filters what we have created so simply i will copy these two lines go here go inside this config folder filters.php and inside this filters.php add those two lines at header next we need to add auth and no auth inside this aliases go here search for aliases and add those two lines right here inside this array why we have added because inside our controllers file inside route file if we go inside routes.php as we can see that we have used filter equal to auth and filter equal to no auth these two are the keywords or let's say aliases name these are the aliases let's say no earth is the alias for this new earth class and no earth has its own code because it is a filter go to blog article we need to create a register custom rule and this is the rule to check a valid email and the password this is the rule for user validation it means when we put the login credentials inside our login form so by the help of this rule we will validate that user credentials are valid or not that user what entered the detail inside database exists or not so simply i will copy this command back here pressing enter it has created a validation folder inside application directory and inside that validation folder we have a file called userrules.php back to editor close all these files first app directory validation and here we have the file what i will do next simply i will copy the whole code and replace with this new existing code now inside this class file called user rules we have a validate user method and inside this method all these parameters we are passing so inside this data array variable via we will get email address and the password at the first step simply here we are loading or creating a user model instance by the help of this module instance we will check that if we are passing the email address inside login form that email accessed or not if suppose that email does not exist we are returning false value otherwise we are comparing password what we have inside of our database go to blog article now next we need to create or simply this is the validation what we have created custom validation now next we need to register this custom validation inside application otherwise we will not allow to use this rule what we have created so to register go inside config folder we have validation.php file and add this class at this line at header go to editor config folder here we have validation.php and inside this validation.php inside this file basically we register all the rules what we have inside our application so right now we have created one custom validation called user rules so we need to register inside this validation.php here we have a question question is that why we are registering this validation and also earlier we had discussed that we have two created two filters called auth and no auth so why we have registered inside this filters.php it is because we are creating custom like filters custom validation rule but to use an application first of all we need to tell our application that we have some filters and some custom validation rules so for registering those things here we have the filters to register of filters validation to register custom validation rules so here we have imported now next we need to write this line inside this rule sets array so copy search for rule sets and passing it here now next application controller settings as we had discussed earlier that we need three different controllers first is all about user controller inside this user controller we will have login and logout method pressing enter user controller created successfully copy this admin so this aspen controller is handling all about admin routes and this is editor editor controller will handle all about editor routes so successfully we have created three different controllers all the created controllers we will find inside this controller's directory so here we can see that we have admin controller editor controller and user controller so one by one we will work on each controller so first of all if i back to blog article this is all about for user controller so what i will do i will copy the whole code of this user controller and here we have slight mistake inside this article is that while running this command actually it is creating user controller not only user only so here we need to update its user controller.php and also here usercontroller.php and also with the class name so after this video i will update this article too so i will copy this whole code go here first of all let's open up the controller called user controller pasting these all updated code and the next we need to do the changes called user controller inside this user controller we have a login method setting all about session values and here we have logout method inside this login here we are checking with the get request type and post request type because inside routes.php the same method is used for get request type and post request type simply to render our layout it will use a get request type and at the first time this method will load login.php view file but when we pass our credentials click on submit button it will go and hit this block called with the post request type here we have the email address and the password what we will take from our login form inside this password look at our custom rule what we have added called validate user inside this method we are passing email address and the password and by the help of this method actually we are checking that both those two details are valid or not so in case if it is not valid we will get an error called email or password don't match otherwise we will able to login go inside here creating a model instance first of all setting all about session variables and inside this method inside this set user session as we can see here we have a variable called is logged in and by the help of this variable actually we are checking in our filters also inside our session we have one more variable called rule and inside this rule we are checking inside our know what filter that the rule is admin or it is an editor so this is the use of setting session values here after setting our session values we are checking the user role if it is admin go inside this admin route if it is an editor go inside this editor route and inside this user controller we have one more method called logout and simply this method will destroy all the session values what we have set here and after destroy it will redirect the user to login route and one more update we will do something we will change this message and the message is something email or password that is didn't match actually these are the things these are the changes also i need to do inside this article so after this video i will update so this is all about user controller if i scroll down next here we have auth controller sorry admin controller so i will copy the whole code go and open up the file called admin controller dot php remove this existing code and paste this updated code so right here inside this admin controller we have a construct method inside this construct method here we are checking that inside admin controller the user role is admin or not if suppose the user role is not admin so in that case we'll we will not allow to access this admin controller because this controller is going to handle our admin operations and all admin operations should be accessible for admin only so other than admin user role they said this will be the message called access denied otherwise inside this controller we have one more method called index it will run the dashboard.php view file again go inside this blog article next we have a data controller so i will copy the whole code open up the file called editor controller replace with this existing code again inside this editor controller we are checking that this controller should be accessible only for editor if the user role is not equals to editor then this message will display called access denied one more method we have called index and by the help of index method we are calling this view file called editor means from the folder editor we have the file called dashboard.php again go here now we need to finally set our application view files so we need few folders let's say here for this dashboard page in case of admin we have admin folder for an editor we have a detail view folder so go inside this views directory creates few folders that is first for the admin called admin and we need one more folder called editor and this is not actually i think we have created something wrong here so let's create a folder go inside this views folder create a folder with the name editor and we need some view files inside these respective folders so first of all we need to create a parent layout so apparently we will create inside layouts directory so we need to create a layouts folder inside this views directory let's see layouts inside these layouts we need a view file called app.php and the app.php contain this whole code so copy go and paste it here simply inside this view file this is all about html code here we have imported some bootstrap links and simply this is dynamic placeholder for the body content this code means that the same layout app.php will be used for login for our dashboard view files for this admin and editor we will use or extend this parent layout back to blog article now next we have we need to create a login view file so go inside this views folder and create a file called login.php so inside this view directory i am creating a file called login.php so i will copy the whole code from this file go here and pasting it here this file indicates all about a login form inside this file we have a form we have email address and password otherwise in case if we get any error here we have a code to display all about validation errors and as you can see that we are inheriting our parent layout something called app.php which is inside our layouts folder inside this body section which is for the dynamic placeholder we are replacing dynamically from this piece of block of code which is written inside this body section so this is all about for login dot php now next we need we need for the dashboard and this time this is the dashboard.php for admin so go here and let's see that go inside this admin create a view file with the name called dashboard.php and inside this file i am writing this piece of code simply this is a view file in which we will have a message called hello admin name and will have a logout button now next i will copy this piece of code for dashboard.php of an editor go inside a detail directory we will create a new file and this will be something dashboard.php and inside this file the same piece of code hello editor name and we will have a logout button again go here and i think all we have done we have covered all these sections step by step of this article now we need to execute our application so after doing all these things we have set if we discuss one more time firstly we have created an application connected with the database created a user table seeded dummy data created few filters registered those filters created some validation rule registered that validation rule created a model configured and finally we have created controller and the respective view files so these are the steps what we have covered inside this video now next let's start our development server to test our application this is not php artisan php spark serve pressing enter and as you can see that application has started so if i open into web browser and if i type the url called login and by the help of this actually we are hitting get request type so by the help of get request type inside login method login form rendered now inside this login form we need to pass credentials so if i scroll down inside application testing if we pass with the details of admin and inside this article already we have created some dummy users so this is the detail for our admin so what i will do simply i will copy this email address pass it here password in this case password is from one to eight so i will paste it here click on submit button and as we can see that we are on the admin dashboard if we try to open a data url here type editor as we can see access denied because this time currently we are logged in with admin details so when we press logout button it will call logout method of user controller and destroy all the sessions and successfully user now redirected on this login page now this time if we pass this detail of an editor copy this pasting it here password is from one to it paste it here click on submit button successfully as you can see that now we are logged in with that detail and this is an editor if we click on here let's see admin press enter access denied why access denied because we are logged in with the editor details so if we click on logout button now we are at the login page and without login if we type the url called admin it is redirected on the login page and this is handled by our auth filter so this is the guys video demonstration of multiple role wise login inside a code igniter for application for this video session guys thank you for watching and have a great day
Info
Channel: Online Web Tutor
Views: 1,784
Rating: undefined out of 5
Keywords: learn codeigniter 4, step by step codeigniter 4, codeigniter 4 tutorials, codeigniter 4 beginners tutorials, learn codeigniter 4 framework, learn codeigniter 4 step by step, codeigniter4, online web tutor codeigniter 4, codeigniter 4 framework, online web tutor, advance codeigniter 4 tutorials
Id: PXxVJXr8dew
Channel Id: undefined
Length: 30min 23sec (1823 seconds)
Published: Mon Apr 05 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.