Best way to make custom authentication in laravel 8 | Laravel 8 tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello friends in this video we are going to show you the best way to make custom authentication in laravel 8 with clean codes this video will fix errors we have seen in previous tutorials about this topic we create this video because of our friend requests let's begin just open browser and go to phpmyadmin and create custom underscore auth underscore laravel database just use this database name in env file on db underscore database after this open terminal and run our application by entering php artisan serve command as you can see we are on laravel 8. let's create controller by entering php artisan make colon controller main controller command in terminal then create login function under main controller import main controller class on web.php by adding use space app backslash http backslash controllers backslash main controller line create login route let's create view for this link bootstrap to this page continue build login ui by using bootstrap classes login form will contain email and password input fields as you can see our login page ui has been completed let's also create register function under main controller class then create register route in web.php and create register view this register form will have name email and password input fields as you can see register page has been completed let's update this anchor tags as you can see our pages work fine let's create save route then use save root name on register action attribute don't forget csrf on this form back to main controller and add save function to it let's check if we can get requested inputs in this method good we can get requested inputs values on this function let's first validate our requests name will be required email will be required and be an email format and password will be required with minimum length of 5 and maximum length of 12. in order to maintain last entered value on input field here we can use old helper function but for security purpose don't use this on password field and also we can add error span that can display error on each input field as you can see error has been displayed under each input field and also we still have last entered value on input field so then go to terminal and enter php artisan make colon model admin dash m let's modify admin table structure through migration file then run php artisan migrate command to create admins table as you can see our table has been created and let's add this table name to our email validation rule if we need to make this email unique after this let's add admin model on our controller by typing use space app backslash models backslash admin line let's try to insert data into database by using model way then if dayton not inserted into database display error message on register page but if data inserted successfully display success message on register page let's display these messages on register page let's test as you can see we get success message let's check if data inserted or not good data has been inserted successfully but let's make this password be hashed to do this first we must import hash class on this controller by write use illuminate backslash support backslash facades backslash hash line and use this hashmake helper on this let's see if works or not wonderful password has been hashed let's test unique email when you try to register with taken email address as you can see we get error message says that the email already taken so our register process has been completed let's make login process now now create check route then use this root name at login form action attribute value don't forget to add csrf on this form back to main controller and add check function to it let's check if we can get request inputs from login form as you can see we get requested inputs from login form in check method let's start validate requested inputs email will be required and be in email format password will be required have minimum length of 5 and have maximum length of 12. let's display validation errors on login page to do this we have to add error span under each input field let's check as you can see errors displayed under each input field let's get logged user info from database according to the requested email by using model way this is query we use to fetch user with requested email from database in admins table if no user founded for requested email let's display error message on login page but if the email is true let's check password if is correct we use hash check helper to compare requested password and password stored in database if password is incorrect then display error message on login page but if password is correct let's store logged user id into session and redirect user to the dashboard area before test let's add this in order to display error message on login page let's test as you can see the email we used is not founded in database let's use registered email but use incorrect password as you can see we get error message that show us the password is incorrect let's use correct email and password as you can see we redirected to dashboard page let's create this dashboard page create dashboard route [Music] then create dashboard method do let's build dashboard page layout on this dashboard page let's create table where logged user info will be displayed back to dashboard method and pass logged user info on view here we fetch data from database where user id is equal to the logged user id stored in session then pass this data to this view so let's use this logitech info variable on dashboard page table here we want to display name and email of logged user let's test as you can see we have now logged user info displayed in table let's make this logout anchor tag working create logout method under main controller class destroy logged user session if exists and return user to the login page let's create logout root on web.php file change it to logged user let's test again as you can see our logout works perfectly but we have problem if we try to visit dashboard page without logged in how to prevent having access on dashboard or other pages without logged in to solve this we have to create middleware to protect routes we want so enter php artisan make colon middleware auth check command in terminal this is auth check middleware php file let's register this middleware in kernel.php file to make clean roots let's create root group where we will apply auth check middleware on it so [Music] so let's move all routes we need to be protected by this auth check middleware inside this root group let's add these conditions on our authcheck.php file this condition will check if no logged user or authenticated user session and user try to access other pages not login and register then this will redirect that user to login page with error message says you must be logged in this second if condition will prevent logged user or authenticated user to access login or register pages let's test suppose we are on login page and we are not logged in let's try to visit admin dashboard page as you can see we can not have access to that page suppose we already logged in let's see how this condition works if we try to visit login or register page when we already logged in we redirected back instead of going to login page also if we try to visit register page when we already logged in we redirected back suppose we click on logout link if we try to go back on previous page which was admin dashboard this will work but this is mistake because we logged out already how to prevent going back to previous page after logged out when you use browser back button to solve this problem we have to use these headers tricks [Music] so let's test now we already logged in on admin dashboard page if we logged out and try to go back through browser back button we will not go back and get error message that says you must be logged in let's create more root inside our root group add some links on this page [Music] so let's test all pages suppose we are not logged in if we try to visit admin profile page this will not work we redirected back with error message says you must logged in this will be applied on other routes let's sign in now when we logged in we can have access to all pages even browser back button works perfectly but once we logged out we cannot return back to previous page friends this is how you can make your own custom authentication in laravel 8 if you are new on this channel please subscribe in order to be notified if we uploaded new video on this channel thanks for watching this video
Info
Channel: Irebe Library
Views: 30,022
Rating: 4.9426112 out of 5
Keywords: irebe library, laravel custom authentication, custom auth, custom auth in laravel, laravel 8 custom authentication, laravel 8 custom auth, laravel 8, laravel 8 tutorial, laravel 8 tutorials, laravel tutorials, laravel 8 multi auth, laravel 8 custom login
Id: ko4PU4eplnY
Channel Id: undefined
Length: 36min 55sec (2215 seconds)
Published: Wed Jan 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.