CodeIgniter 4 Authentication | Login, Register & Filters | Codeigniter 4 tutorial [HD]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello friends in this video tutorial we are going to show you best way to make user authentication in codeigniter 4 don't miss to watch this video tutorial if you are a fun of codeigniter php framework from this video you will learn how to make form validation with a custom error message and make controller filters in order to prevent direct access to certain page without logged in let's get started just make sure you started apache and mysql on your xampp go to phpmyadmin and create database rename to code a nighter underscore authentication under code a nighter underscore authentication database create table and rename to users this is the structure of users table back to vs code and rename this file to env after this uncomment ci underscore environment to development mode also uncomment database info and change database.default.database to code a nighter underscore authentication username will be root and password will be blank then run our project by entering php spark serve in terminal so use this url to run it as you can see our codeigniter project is running into browser let's see how to make controller by using codeigniter user guide copy this code then create our first controller and rename to auth.php then paste copied code and change class to the same to file name which is off let's check if it works in browser as you can see auth controller works perfectly let's add login form to this page then add return view on this index method create this view inside views directory this page will contains login form let's link bootstrap on this page as you can see bootstrap has been linked to this page let's continue make ui with bootstrap classes login form will contains email and password input fields do let's test as you can see our login ui has been completed let's create register method under auth class this method will return view that will hold register page this page will contains name email password and confirm password input fields let's test as you can see also register page has been completed let's add some anchor tags under each page as you see on left bottom when we hover on this link we have index.php included on our url so when we click on it we have the url that has index.php how to remove that index.php from our url let's see how to do this from user guide just copy these codes and create dot ht access on the root of codeigniter project then paste that code you copied also go to the app config and open app.php file and remove this index.php on this line and save let's test let's try to remove this index.php as you can see we don't have any error also our links work well without add index.php on them friends as you can see both login and register pages has been completed let's start register process back to auth controller and add save method to it then add this action attribute to the register form let's test so we can get requested posts from register form in save method let's add csrf on this form as you can see we have csrf input hidden field on our form let's create model and rename to usersmodel.php inside models directory this class name must be identical to the file name just define table name as protected define id as primary key then after define allowed fields so let's back to save method and start validate our requested inputs from register form name must be required email must be required valid email and unique in users table this is table name and this is table column name password must be required with minimum length of 5 and maximum length of 12. confirm password must be required with minimum length of 5 and maximum length of 12 and matches to the password so if form is not validated let's display validation errors on register view otherwise let's display success message let's test if we submit form we redirect back to register page because we have validation errors let's display errors on each input field on this page in order to make clean codes let's create custom helper that will help us to display errors on each input field on register form just create form underscore helper dot php file under helpers directory inside this file let's add these php scripts let's use this helper on register page let's create error span and give it the text danger class and inside this let's use display underscore error function this is the validation errors from controller and this is the name of input field this must be the same in order to use this helper let's define in underscore underscore construct function above all auth class methods let's test as you can see we have name error that show us that the name field is required let's continue add error span on other input fields just rename display underscore error function second argument to the same as input field let's test as you can see we have all validation errors displayed under each input field but if we submit our form the last entered input field value will be removed how to maintain last entered value on input field we have to add set underscore value helper on each input field value attribute let's test as you can see john still displayed on name input field we can still having old entered values on each input field after from submitted but we have problem as you see on the confirm password this is not good error message text for users how to customize the validation errors let's comment this validation let's create another validation that will help us to make custom validation messages so do so so this is the validation with custom error messages let's test as you can see we have new customized error messages here so so let's insert data into database if form is successfully validated let's store requested name email and password in these variables these are the columns names of users table add user's model on this method then write query that insert data into database this line will insert data into database so if dayton not inserted into db let display error message on register page otherwise display success message that shows us the user registered successfully let's test so we don't have any error let's check if data inserted into database as you can see new user inserted into database so how to display success message on this page to do this let's add these codes here if we get fail session flash data we have to display error message otherwise display success message if we get success session flash data let's test good we have now success message on this page if data inserted successfully but here password is not hashed how to hash password just go to app libraries and create custom library and rename to hash.php inside this hash.php file let's define namespace to app backslash libraries then add hash class this class name must be the same to the file name let's create first public static function inside this class and rename to make this function will help us to hash password to use this library we have to import it on the auth controller before insert password let's hash this password by using hashmake function let's test as you can see password has been hashed let's use this user as default user on login process let's create check method inside auth controller then give the following action a tribute to login form let's check before everything let's validate our requested inputs email must be required be a valid email by checking on user guide on validation topic we have here a powerful validation rule is underscore not underscore unique this will check a give value if exit this means that this value must be exists in database table so this email address must be exists into our users table remember this is table name and this is table column name for required rule let's display email is required message for valid underscore email rule let's display enter a valid email address message and for is underscore not underscore unique rule let's display this email is not registered on our service message password must be required with minimum length of 5 and maximum length of 12. for required rule let's display password is required message for min underscore length rule let's display password must have at least five characters in length message and for max underscore length rule let's display password must not have more than 12 characters in length message so if we have validation errors let's return login view with validation errors let's display error on each element by adding this span error under each input field so we have an error this view was wrong change to auth slash login as you can see we have validation error on under each input field in order to maintain last entered value we have to add set underscore value helper on email input field let's use registered email address so our form is validated then add this for displaying error message back to check method and get user info according to the requested email address this line will fetch user info according to the requested email address back to the hash.php library file and add second public static function that will compare requested password and password retrieved from database users table to use this function let's check those two passwords then if both passwords not match display error message that shows us the password is incorrect let's test let's use wrong password as you can see we have incorrect password error message here but if we enter real credentials we don't have an error message if user enters real credentials let's store his or her id into logged user session and be redirected to dashboard page let's create dashboard controller then create dashboard view under views directory let's test as you can see we redirected to dashboard page let's design this page and display logged user info on it just link bootstrap on this page let's design this ui by using bootstrap classes back to dashboard controller and get logged user info then passes them to dashboard view so let's display past data to dashboard page let's test as you can see we can see logged user name and email on dashboard page let's create logout link then create log out method inside auth controller class if we have logged user session remove it and redirect user to the login page let's test as you can see after click on logout link we redirected to login page let's see if we try to log in with no registered email address as you can see we have error message that shows us the email is not registered but if we use registered email and wrong password we have an error message that show us the password is incorrect the if we use correct password we redirected to dashboard page and if we click on logout link we redirect it to login page let's register new user then try to use registered email address as you can see we have error message that shows us the email is already taken so we have new registered user if we try to use this user credentials we also redirected to dashboard also logout works perfectly friends as you can see login and register process have been completed and worked perfectly let's change this password type and change to password type in order to prevent input field autocomplete when you type let's add this autocomplete attribute to our forms let's test as you can see when i'm typing there is no autocomplete appears friends you can also redirect user to dashboard after registration process done how to do this in save method instead of return user to register page after registration let's redirect user to dashboard here we can get last inserted id and store in dollar last underscore id variable so let's test as you can see user 4 has been redirected to dashboard after registration suppose we are on login page if we try to access dashboard without logged in we have this error how to prevent no logged in user to access dashboard or other pages without logged in let's see in user guide how to use controller filters just create auth check filter.php under filters directory then change this class name to the same name of this file this filter has two functions before and after for us we will use before method so inside before function let's redirect user to the login page if is not logged in to use this filter we have to register in appconfigfilters.php file let's give an alias of auth check this word must be the same to authcheckfilter.php file classname then back go to roots.php file in order to protect some roots we have to put them inside roots group and apply created filter on this root group let's add some roots here for us let's add dashboard root inside this group let's test if we try to access to dashboard page we redirected back to login page because we don't logged in let's create profile method do so if we try to access dashboard profile page we have also error because this page must be accessible by logged user only how to solve this problem just add also dashboard profile root inside our group let's test if we refresh browser as you can see we redirected back to login page but if we logged in we can have access to both dashboard and profile pages suppose we are logged in if we try to access login page we can back to login page even if we logged in but this is problem also if we try to access register page this will work too how we can prevent logged in user to access login or register page to do this we have to create second filter file and rename to already logged in filter.php then change this class name to the same of file name here in before function we redirect back user if he or she try to access login or register page when is already logged in then register this filter inside filters.php file in order to user this filter let's create another roots group then add both login and register routes inside this group let's test if we try to access login page automatically we redirect it back also if we try to access register page we also redirect it back friends everything works well so this is how you can make user authentication in codeigniter4 you can find examples of how to do some tasks i codeigniter for user guide if you are new on this channel please subscribe in order to be notified every time we uploaded new video on this channel thanks for watching this video
Info
Channel: Irebe Library
Views: 20,882
Rating: undefined out of 5
Keywords: irebe library, codeigniter, codeigniter tutorial, codeigniter 4, codeigniter 4 tutorial, user auth, auth, user authentication, authentication, codeigniter auth, codeigniter authentication, codeigniter 4 auth, codeigniter 4 authentication, codeigniter auth tutorial, codeigniter 4 auth tutorial, codeigniter 4 authentication tutorial, codeigniter 4 login, codeigniter 4 register, codeigniter 4 login register, codeigniter sign in, codeigniter 4 registration, codeigniter filter
Id: vKFcpQo-h-Q
Channel Id: undefined
Length: 60min 22sec (3622 seconds)
Published: Sat Feb 20 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.