Laravel login with username instead of email | Laravel 9 Login / Register

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] [Applause] [Music] hello and welcome to tech tool india in this video i am going to explain you how you can use username and password for login using laravel authentication scaffolding if you are coming to this channel for the first time please do subscribe our channel to never miss any video on laravel so without wasting any time let's get started i have already installed laravel 9 on my machine if you want to know how you can install level 9 you can check out the video in the description so we have installed laravel authentication scaffolding which will comes with login and registration feature if you go to the login it actually accepts the email and password and if you go to the register it will take name email password and confirm password to register a user so what in this video so in this video what i'm going to do i'm going to explain how you can take user name while registering and use username as a login credential with username and password for login so without wasting any time let's get and update our register form to do that what we need to do we need to jump into the code we have our resources folder views folder inside that we have our auth folder and if you open this register.blade.php file you'll see we have name email password and confirm password so what i'm going to do here after name i'm going to copy this name field and paste it here and name it as username right so i'm going to give a name of username so let's call it username here and just to distinguish we'll give it username field and id will be again username and name also will become username here form name field name input field name is name and again for errors and then so let's go in our form refresh this you can see we have one more field called username right now if you see our users migration table it has name email email verified password remember token it at an updated ad but it doesn't have any username field to add a username field in an existing table what we need to do we need to create a migration which will add a username field just after name so let's go ahead and create our migration file to create a migration what we need to do we need to run a command phpartition make migration add username in users table i'm giving this name to migration as soon as i hit this command it will create add username and users table file where inside database migrations folder right so what we need to do here we need to add our username field so what i'm going to do here i'm just going to add a field which will be a string and the name of the field is username and this should be after name field i think that's it it's just like we need to add a username which is a varchar and it will be after name in the down function what we need to do we need to drop this column username when someone roll back this particular migration right so we completed our migration to run this i am going ahead and just running this command migrate php artisan migrate so this has been run successfully let's go in our database refresh this as you can see we have one username field which is non label the next thing which we need to do here is to update our user model right let's open user model here in refillable we need to define another field which is username so we have defined this username as a fillable property so that when we are registering username will be available right let's go and check out our register controller go inside app http controller auth and then register controller in register controller if you see we have validation for name email password and confirm password let's add a validation for username as well here so i have added a validation for username which will be a string required and maximum 255. that's fine so we have added our validator and now while creating what we need to do we need to insert username as well while creating our user right so we have updated this as well let's go ahead and check our registration function if it is working or not so i'm just using this as a test user one as a username and testuser test.com i am taking password and let's register it once you register you can see like we have successfully registered and login in our dashboard if you go to database and refresh it you'll see your username field has been there in database so let's log out now if you try to login what it actually accepts it accepts email address and password so i'm going ahead and checking with our email and password and it's working so now what if i want to login with username and password so in order to achieve that what we need to do we need to understand first how the login is working for us so let's go into code open our app http controllers and auth controller in that we have login controller when you go to the login controller you'll see we have a trait which is authenticate user if you open this street there is a function called username so let's find if we get this username field right so here it is actually a function so if you go ahead and find the username so if you look at this get the login username to be used by the controller so in username it actually returning the name of field which should be considered as a username right right now by default it comes up with the function of username which returns the email so what you can do you can simply copy this function and in our login controller we can override our trade function so let's add this username function here and in username function instead of email i just write it username of course whatever field you are writing here in username function should be a part of users table then only it's going to work as we have updated to username let's go and check out our login function so as i go and login now if i try to login with email address it should fail right because we have updated so if you see it's not working but what if i just try with my username right the username was testuser1 and i'm going ahead and entering the password and click login so as we have this email field so we need to update the email i mean login form as well so let's go ahead and open our login form in order to open the login form you need to go into resources views and then auth here you have the login.blade.php file right the first field is input of email so instead of email we need to do what we need to do here we need to write a username here because we are taking now username and everything where we have email should be replaced with username right and in type we'll just take as a text right cool so we have updated our login form let's go ahead and refresh our page now you can see in a login form we are accepting the username and password so let's update this username and in password i'm going to enter the password and i hit login button you see you can log in with your username and password by just updating a simple function in our login controller which is username right so it's really simple all you need to do you can use any of the field of your users table either email or username or mobile number or any other field or unique id or something like that so it's very simple you just need to add a simple username function in your login controller and written the field name into that function and it will enable that login with that particular field name right so i hope this video is informative for you if you like this video please don't forget to subscribe our channel do share this video with your friends if you face any issue while implementing this please do comment out your issues i'll try to solve all those till the next video keep watching keep learning thank you for watching [Music] you
Info
Channel: TechTool India
Views: 5,315
Rating: undefined out of 5
Keywords: laravel, laravel login, laravel login with username or email, laravel login with username instead of email, laravel login and registration, laravel tutorial, laravel 8, laravel login tutorial, how to login with username in laravel, login, login system in laravel, laravel login with username, laravel authentication, laravel 6 login with username, laravel 7 login with username, laravel 5.3 login with username, laravel login system, username login in laravel
Id: y0HBjmS4Kho
Channel Id: undefined
Length: 11min 38sec (698 seconds)
Published: Fri Jul 22 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.