How to change password in laravel 9 | change password in laravel 9 for beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello and welcome to tech tool india in this video i am going to explain how you can perform change password in laravel 9. if you are new to the channel please do subscribe this channel for more reliable update so without wasting any time let's get started as we already installed laravel and if you want to see how we can install level 9 you can click on the i button or get a link in the description so to perform a change password we must have logged in into a system so i already created a user using that credential i have just logged in as i am inside the system we need to have a route where we can have this change password screen coming on the dashboard so let's go into the code let's open our root web dot php file if you see this is a home screen so we need to make a new route for home controller let's copy this call it change password in the home controller we need to make a new function name change password and name this route as a change password all right so let's save this now we need to create this change password function inside a home controller let's open the home controller file and we have to make the change password function inside a home controller so what will we do we need to write a function we'll name the function as a change password and what we need to do in this function we need to load a change password file so let's create a change password file blade file let's go into resources views and inside views i'll create a new file called change password dot blade dot php all right so i'm going ahead just copying all the content from home pasting here and renaming this as this is change or let's rename this to change password save this inside a home controller inside our change password function what we can do we need to return this change password all right now as we we have to link this route somewhere on the application so let's go to the application we can link here just above the logout link so let's go into our layout file in app.layout you see we have a logout link once we are logged in if it's not logged in we will see this login and registered link and if once we logged in so you will see the name of authenticated user and the logout link let's copy this logout link and give it a change password here give it a reference of change password name of the root of change password let's remove this complete part save this file go to our browser refresh it and see we have this change password if i click on this you can see here we are now inside a change password so here we want a form so i'm going ahead and copying a bootstrap 5 form from bootstrap documentation so here i have opened the bootstrap 5 documentation from here i am just copying this form and pasting this form inside a change password let's first create a form and inside form i'll paste that copied form which i have copied from bootstrap 5 let's little bit style it rename this to change password now let's go to the browser refresh this page you can see we have our form here let's rename all the field to change password we need three inputs old password new password and confirm new password so let's create three field here let's update all the input so our form let's give it action and method method would be the post action we will create all right so our form is ready what we need to do next we need to have a route to submit this change password form and handle this validate the old password and match the new password and confirm password apply a validation and change the password for that we need to create a root first let's go into web.php copy this make it post method change password let's rename it to update password name it update password let's copy this function name go inside a controller create a new function update password which will accept a request form request so we'll create that so what we need to do here first we need to create a validation second match the old password and third would be update the new password so let's update the form and see if we are getting all the data or not so for debug purpose what we'll use dump and die and i'm just dumping all the data coming from the request so let's update our form inside our action we need to give a root of update password let's go ahead and refresh our page add some random i'm just adding some random numbers here and submitting the form as you see this page expired because we haven't updated a csrf token in our form so every form in laravel needs to have a csrf included inside it let's go ahead again refresh it put some number there hit submit as we have dumped all the data so you'll see we have a csrf token old password new password and new password confirmation so let's what next we need to apply the validations let's go into the controller function next we need to create a validation so to validate this what we'll do we'll create this validate function and inside this will write our rules for old password this should be a required field similarly we'll take a new password new password is required and then we'll use confirmed so what it will do the confirmed rule will check for a field called new password confirmation as we have our field this third field for confirm new password we have named it like new password confirmation so it will validate this field as well let's create let's use this and take our dump and die just below the validation so it needs to pass the validation first and then it will dump the data let's go ahead and refresh this form you'll see we will redirect it to this but we don't see any messages so to display the error messages what we need to do here to display the error message on blade we need we can use blade directive error to display this let's add this inside our code let's go into a blade file and just below our input file if i use this directive and see it old password and inside this i'm just using a span with class danger text engine and inside this i need to print the message let's see if it is working or not let's go ahead and let's go ahead and refresh this form submit this you see the old password field is required so the validation is working now if you want to add a class on this so let's go ahead here inside our input let's use our error directive again and if it has the old password add a class of is invalid i think this is going to work and it's going to add a class is invalid if there is a old password error let's go ahead and refresh this you see now our error backend error is displaying on the form let's apply for all now you see the new password and the old password field is required let's add some data here again here it the new password confirmation does not match so let's add a same password now you see we have the same password for new password and password confirmation so our validation passed and we have dumped the data after that i'm just creating this basic validation in the next video maybe we can see like how you can apply your own custom password validation and there is a method available in level 9 where you can you know use the password validation very effectively where you can see like it should be eight character it should contain the alpha numeric and then this we will see in the next video but for now i'm covering this change path let's go ahead and add the next thing in the code the next thing which we need to do is to match the old password for that what we need to do for that we need to check our hash which is stored in the database with the old password let's create if condition and inside our if we'll use hash check and the value which we are getting inside our old password and i'm going to use as the user is authenticated so we can pull our hashed password from database what we need to check if our old password doesn't match with the saved password we need to give an error let's take if it doesn't match we need to dump a message old password does not match okay let's print this if it doesn't match go ahead and refresh this as you can see the old password doesn't match so what is the old password let's go ahead refresh this form and use this password which i have saved in my browser now i need to enter a new password submit it now you can see our password match with the old password and our validation dumped here if you see here you can see the status if status is there it prints a success message similarly what we need to do we need to add one more condition here just below that in inside else if condition if there is an error so we have to print the error as well okay let's go ahead and once it doesn't match so we need to return this message in written back with error and the message let's go ahead go back to this i'm just entering some random values and entering new password confirm new password and submit you see old password doesn't match so instead of alert it should be danger okay let's again give it you see now old password doesn't match so we are validating it properly let's update the old password properly and enter a new password with matched new password and you'll see we have all the data there in the controller so the last step we have to do is to update the password right so uh for updating a password what we need to do update the user where id is equals to auth of id and then call this update function inside update update the new password so for that we need to use our hash make function and we have to pass this new password right which is coming from the request and that's it i think we need to import the user model here i have imported this and once this is done we need to return a success message so what we will do we need to return this and the message would be password changed successfully right let's do that go ahead refresh this and so instead of auth of id what we can do auth of user of id so let's do that we are inside our change password i'm going to use the old password entering the new password and submit this password change successfully if i use the old password again and try to use the same thing it should give me the error and you see we have the error now so we need to give the new old password and then update it with any other password and submit it it's working so uh it's very simple i just tried to explain it simply with the every step you need to follow in order to change the password inside a logged in user dashboard if you have any query you can comment down to this video i'll try to answer your queries if you found this video informative please do subscribe this channel i will keep posting new videos on level 9 in the coming weeks till the next video keep learning keep watching thank you for watching [Music] you
Info
Channel: TechTool India
Views: 5,987
Rating: undefined out of 5
Keywords: laravel, laravel change password, laravel reset password, change password, laravel change password in database, change password in laravel, laravel forget password, laravel 8, laravel change password tutorial, laravel 9, laravel tutorial, change password in laravel 8, laravel 8 tutorial, how to change password in laravel, laravel change password encryption, laravel 7, laravel 6, change password functionality in laravel, reset password laravel, laravel forgot password
Id: IM1bvPzJtTE
Channel Id: undefined
Length: 19min 44sec (1184 seconds)
Published: Wed Mar 02 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.