How to Reset Password in Laravel

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we'll be seeing how to add custom forget password option to lrl project so we'll be doing this as a continuation of the previous video we have done a video on how to create login and registration in larval this is a custom login and registration so you can follow this for creating login and registration so after that it will be like this we'll be having a login and registration form as you can see here okay now we will be implementing the forget password option so if you go to the database and check you can see that we are having one user already registered and I have forgotten the password of this user so we'll be seeing how to recover this password so if you don't know how this table was created and all you can simply watch this video I'll give the link for this video on the description so this will show you how to create a login and registration form so let's get started with the implementation part so here you can see that we are having a login form so we'll be adding a link to the forget password first of all we have to add a route so go to the routes and add a route for forget password we need a get route to the forget password we'll be showing an option to enter the email address so that we could send the link to reset the password so that's what we are going to do in this route so for that we have to create a controller so let's give a semicolon here so in order to create a controller you have to remove this line so let's cut this line for now and let's open the terminal okay you can see that I'm having the server running okay so let's create a new terminal and let's create a controller we have to change the directory to the project root folder so you can scroll to the top and we can see the custom login as our project folder name so you can type CD custom login now we are inside the correct directory and you can create the controller PHP artisan make controller forget password manager the name of the controller will be forget password manager okay now we can hit enter you can see the controller is created on the folder controllers you can see the new file name open the controller you don't know where the controllers are placed go to the app HTTP and controllers okay that's all now let's minimize the terminal inside this we have to create a function to return the forget password view that is where we enter the email address for that let's create a view go to the Views folder inside the resources you can see views now let's create a new file or get password we will do the designing part of this later on so let's simply extend the layout so if you go to this layout file you can see we are having boost up we have to pass the content here and here we are including the header that's all so that's it so we'll be doing the designing part later now we can go to the web.php the route part and we can create the route for this now we can go to the controller and create the function let's give the function name like forget password now inside this we'll be returning the view that's all okay now we have to call this function so go to the web.php route path and create a route hold forget password route and we have to create a get route okay get slash forget password okay now we can call the controller so here you can see it is in gray color you can simply click here and type old tender this option is only available on phpstorm so if you are using any other IDE you can simply add this line here see that's all we can also give a name for this route we also need a post request to this same URL so let's duplicate this line and change the get to post okay now we have to change the function name also so let's name the function forget password post we also have to change the name of the route so let's change the name let's add note post Tweet okay now we are having a post request also and we have to Define this function so let's copy this function name and go to forget password manager and here you have to create the function this function will be receiving data from the form so we have to use request okay now to make sure that this request is from the illuminate HTTP request class otherwise you will not get the correct result will you paste errors also okay now we are having all the data that we pass from the form in this variable okay so before implementing this function we have to implement the form so go to forget password.play.php and we have to create a session for content now we can end this session at the bottom now inside this we have to create the form so we already have a form in the login.play.php so let's copy that form so this is all so we'll be copying this form and pasting it inside the target password okay that's all so before copying you have to create a main tag and inside that we can also create a div now inside this you can paste it okay that's all so this form just contains an action and we have to change the action to the new one that is you have this name okay you have to use this name forget password post so copy this name and we can replace this name here okay okay now we are making a post request yes that's correct okay we are having a width for the o okay let's change the margin top to three let's make it five okay and okay we are having the csrf also we are having the email and password we don't need the password so let's remove the password we only need the email option we can also show a message on the top so for that we can copy these classes from here cut it from here and you can paste it on the div we are simply having some margin to align this to the sender and we are also having a width for the div tag that's all okay inside this div we can add a paragraph and let's say that we will be sending a link to your email so you can use that link to reset the password some message like that okay that's all now we can go to the forget password page and let's check so the route name will be slash forget hyphen password okay you can see the form okay now we are having the email option and we can enter the email and submit it okay now we can go back to the PHP store we also need to display arrows so for that also we have done something that is this part we can copy this from here and paste it here I will explain what that is so you can see that we are showing the errors here now if there are any errors we'll be printing them in a forage okay you can see that we are printing it in a forage after that we are checking if the session error is set and if it is set we are showing this alert and then we are checking if the session success is set and if it is set we'll be showing this success alert okay that's all now we are done with the forget password blade part now you can go to the Post so go to the forget password manager and go to the function forget password post now we are receiving the email here we have to validate the email for that you can use dollar request validate inside this we'll be passing an array so email now you can add certain validation to the email so let's make the email required and we can set the option to be email it can also give an option to check if the user exists or not so for that exits colon users now this users is the table name okay so you have to make sure the table name matches now you can give a semicolon now that's all now you can create a token dollar token equal to Str so you have to choose the SDI class inside illuminate slash support and call the function random inside that you can pass the length of the string now we have generated the token we have to store this token and the email address on a table so let's go to the larval database migrations you can see one file named create password reset table okay you can see that we are already having a table password reset and inside that we are having email and token and also created a timestamp so we'll be storing the email and token to this table so if you go to the phpmyadmin you can see that we are already having the table password reset you have to do the migrations first in order to have these tables so by default larval is having all these tables created so we'll be using this email and the token okay so let's insert email and token to the table for that call DB and table inside that we will be passing the table name that is password underscore resets now we can insert the data call the function insert now we can pass an array so we'll be passing email you can get the email from Dollar request of email and you can also pass the token so you have to give a comma not a semicolon okay let's pass a token and you have to pass the token that we just created that is dollar token leave a coma and next we have to pass a timestamp so that is created at you have to use the carbon class inside illuminate slash support okay now this will pass the current timestamp that's all now you can give the semicolon let's scroll down okay okay now this line will insert the data to the table password reset okay you have to make sure that the table name is exactly correct we are having any doubt go to the phpmyadmin and you can see the table name here password resets okay if you are not having that table you simply have to create that table that's all and we need email token and created it that's all okay now we have to send an email to this email address okay so this is the email that the user entered from this view okay now we have to send an email for that you can use the class mail so you have to make sure that the class is inside illuminate slash support slash brackets and the class mail okay and you can use the send function inside that we have to specify a view so inside that view we can do the designing part of the email so for that you can come to views and create a folder now inside this folder emails we can store the templates of emails okay so let's create one for forget password inside this we can design how the email should look like so we need a link to reset the password so we'll be passing that so let's create a link now the a tag should have a link so we have to give an href we have to pass the route so we haven't created the route for this so let's go to the web.php and we can simply create a route here on the bottom now we are having a get request to slash reset password and we are calling this function we haven't created this function yet but we'll be doing that later on we also have set the name for the route that is reset dot password we have to pass a token so we have to give a slash after this and we'll be passing a token so start a braces and we have to pass token now this token will change according to what value we pass so let's create this function so go to the forget password manager and on the bottom create a function okay let's go back create a function paste the function name and that's it okay we'll do this part later on so let's complete the email part right now and we can go to the email template that is this one forget password email template now you can use the route here so call the route and we have to call the name that is this one reset dot password we also have to pass a token you can see that we are asking for a token here so you have to pass the token here so give a comma and we have to pass the token dollar token now this file currently doesn't have this token so we have to pass this token also so for that you have to go to forget password manager and inside this email send function we have to call the view that is emails Dot or get password okay you have to check these spellings here okay this is the file that we are using okay forget okay now we are passing this View and now we have to pass the value to this View comma we have to pass an array token okay you don't have to use the C okay make sure the spelling is correct now we can give the variable that is token okay now we are passing this token to this view okay and we can use that like this okay hope you understand that these are basics of larval so if you don't understand basics of larval you can try to read the larval documentation or search online to get some courses you can simply watch them I'll be also creating a complete lateral course so be subscribed to get notified when I release the course okay now we can create the function so function inside this will be passing the message open the function now inside this we have to pass the message so so we have to use the message and now we have to add the two that is two you have to give the email address of the user we can also pass the subject we have to give a semicolon so we have to change this comma from here and make it a semicolon also give a semicolon at the end here okay okay that's all now we are sending the email to the email address given by the user and here you can set the subject and this will be the content of the email okay this view part okay okay that's all now we are sending the email in this line okay then after that we can return this user to the forget password page so for that you can return redirect or we can use the function that is back now this function back will redirect user to the previous page so this is a function provided by larva so if you want to use redirect we can use redirect also so let's use redirect right now redirect and we have to give the two inside this we'll be giving the route so route and the name of the route so let's check the name of the route okay let's hit the enter here so we can view it properly okay the route name is forget password so let's copy that and we can paste it here forget dot password okay and after this we can pass a message so use the function with now we can give a key so the key should be like success and then we can pass the message now that's all now we are sending the email here now in order to send the email we have to configure the SMTP details so go to the dot EnV file and you can see these details here here you can see that we have to keep the username and password of the mail server so here you have to give the host name now this will be provided by your service provider and the next one will be the port name in most of the cases it will be 465. and this will also be provided by the service provider and now we have to give the username and password so here we'll be giving the username and the password for that account and the encryption will be mostly SSL or TLS also here we can provide the from address and this option is for app name that's all so you can configure these details so if you don't know how to get these details you can simply Google it how to create an SMTP account I'll be using my details here I won't be showing those details to you so you can Google it and see how to create the SMTP account so you have to Simply create the account and give the password and username here that's all so set up these details so I have entered my details so I won't be showing it okay so I have fill the details now we will be able to send the email if you don't fill the details it will give you an error message we can start the reset password function so go to the reset password function and here we have to return a view so inside that view the users will be entering the new password okay so for that we have to written a view for that we have to create a new view here so come to the Views and create a new view so we have created a new view name is new password.play.php okay now let's copy the things from forget password you can copy this much from here Ctrl a and Ctrl C and you can go to the new file and paste it here now we have to do some modifications that is we need the email address and we need two password fields so let's copy it from here copy this field that is the email field and you can paste it two times okay now you can change the type to password the bottom one also change the type to password okay now here we can enter the option like and that new password and the second option is for confirm password okay we also have to change the name so we have to change the names here so let's set this name to password and the second one to password confirmation now we have given the password and password confirmation and also the email option okay now we have to change this route here so we have to create the route for that so for that you can go to the routes that is web.php and we have to create a new route now the route should be a post request and we'll be creating it for slash reset password now we have to create a new function for that so let's name the function something like reset password post we can also name the route okay that's all now we are having a pause request to slash reset password and we'll be calling this function so let's copy this function name okay and we are also having the name for this route okay now go to the forget password manager file and we have to create this function also okay let's create the function function paste the function name so here we'll be receiving the post variables so for that you can use request to all our request okay okay that's all now we also have to pass the view here on this function that is the reset password function so return okay return [Music] View and the view name is new password Dot blade.php so you don't have to give the dot play.php part simply give new password okay that's all so we can also pass the token here so we are actually passing the token so here we have to receive the token and we can pass the token here so if you don't understand the token part go to web.php and here you can see that we are actually passing the token so you can receive this data inside this variable okay and we are passing that to the view that's all so if you are confused why we are passing this token to the view we have to add this token also to the form that we created in the New password.play.php Path so go to the form that is new password okay now we have to add a token also here so create an input tag type will be text and we have to make sure that this is hidden so simply type hidden okay and we have to pass the value also now we can access this variable dollar token here since we passed it to The View so dollar token okay now this won't be visible but it will be there on the phone and when we click the submit button this value will also be passed so we have to give the name for this so let's give the name token okay that's all so here we are passing the token now we can go to the forget password manager and we can start with this function that is reset password post function now inside this function we have to check if the email password and Confirmation password is centered or not so for that call the validate function we can pass it as an array so check email now check the password now we can check the password confirmation now that's all now here inside this password we are making it required and we are also checking if it is a string we also have set the constraint to make it a minimum length of 6 and also we are checking if it is confirmed that means this confirm password and this password are matching or not that's all okay now let's give a semicolon now we have to fetch the data from the table password resets so here we have inserted the data when we created the token anode so we inserted those data here now in this function we'll be validating those data so we have to read all those data now here we are fetching all the data from the table password resets and we can call the function ver so let's hit an Ender here were now we have to pass the constraints now you can pass it as an array also so let's pass it as an array so we have to check if the email and the token are matching now we are passing all those details that is the email and the token and we can receive that inside this variable okay hope you remember that we are passing it from this or you can see that we are having the token and we are also having the email okay now we have to change this route so we forgot to change this route so let's go to the web.php and this is the route name okay reset dot password dot post so copy it and we can change it here that's all so make sure that you change the route otherwise you will get error okay and go back to the function okay you have a coma now we have to pass the token okay here you can call the function first okay so that's all now we have to check if not dollar update password now if there is no data inside this update password we have to return the user and we have to say that an error occurred okay return redirect to now we have to give the path so we have to give the route name so go to web.php and you have to redirect the user to reset dot password so this is the name and we can paste it here okay we can also pass some value as an error message so with now this is an error so error comma we have to give the message invalid so we are passing the message invalid okay now that's all now after that now if the data is present we can actually reset the password so for that we have to call the model user were now we have to give the conditions so email pass the email that is dollar request email now we can update this so update the password so call the function update inside an array we can pass the password and have to give the new password call the hash function which is inside the illuminate slash support slash packets otherwise you will get error so make sure you are inside the exact class and we have to call the make here you can see this in yellow that's because we haven't imported the class so click here and type Alt Enter import class so if you are using any other ID other than PHP Stone you have to add this line okay that's all simply add this line okay and scroll down now inside this make we have to pass the password dollar request or password okay let's see 10 enter here and we can give a semicolon here okay that's all so this will do the update query now you can also check this and if the update query fails you can show an error message so we are not doing that if you want to do that you simply have to give an if condition and if it is false you can show an error message that's all now after resetting the password we have to delete the row from the table password resets so here we are entering the data and after completing the process we have to delete that row so in order to delete we have to call the table name give the condition where he said an array pass email value of the email that is from request email okay now we have done the condition now you can call the function delete so that's all now this line will delete that Row from the table that's all okay now we can return the user to the login page and show a success message okay now here we are passing the success message so that's all with the password resetting part so we have completely done the implementation now we can try running it so let's go to the login page so we forgot to add a link on the login page so let's do that so come to login.play.php so if you haven't done this login part you can simply watch the video I will give the link on the description we already have done the video on login form and registration form so watch that video and you can watch this video after that okay now let's keep the link for this reset password here okay now here we have to pass the route so let's check the route name so go to web.php and we have to pass this route name okay that's all so let's go to the login and refresh the page okay let's refresh the page now you can see a link here with the option to reset password so let's click the link okay now we are inside the reset password option so let's give the email address so I already have a user registered so let's go to the users table and you can see the user email is support at code cc.com so let's copy this email from here and paste it here and we can submit okay now this is sending an email to that email address now you can see that we have successfully sent the email so let's go to the email and check it so we have received the email so you can see we only set the link so you also have to add some text otherwise the chances for this email to end up in spam are high so simply add some text like you are linked to reset the password is here and tell some message like that so let's add some message on the email template so go to the emails and forget password play.php so here you can see we are only having an link so go to the top and let's add a H1 tag and we can give some heading like reset your password and below that we can give a paragraph here you can give some message okay now you can see that we have given some message here okay let's try sending this email once more okay let's enter the email here and we can submit it okay now the email has sent once more so let's go to the Gmail so here you can see the next email and we are having the title and the message here so let's click on the link and reset our password so the link will open to the new password View so let's enter the email okay you have to enter the same email otherwise you will get an error and now we can insert the new password so let's enter one two three four five six okay you have to enter the same password Here Also okay and let's click submit now the password was successfully reset you can try logging in once more so let's enter the email and password is one two three four five six okay let's submit it okay now we have successfully logged in you can see the logout option see and this is the name of the user okay that's all so that's all so let's click log out we have successfully implemented the forget password option so this is how you can create a custom reset password option in laravel hope you understand if you have any doubts you can leave a comment and I will respond to them if you want to download the complete source code for this project you can come to this URL I will give the link on the description and you can download the source code from here if you like this video give it a thumbs up and subscribe to this channel for more such videos
Info
Channel: Codes Easy
Views: 2,953
Rating: undefined out of 5
Keywords: codeseasy, laravel reset password, laravel forget password, forgot password, laravel custom password reset email, reset password, reset password laravel, forgot password in laravel, php forgot password, laravel forgot password working realtime, laravel forgot password, laravel reset password send email, laravel reset password api, laravel reset password send email api, laravel reset password email template, laravel reset password route, laravel reset password controller
Id: xQr5PxbZGZI
Channel Id: undefined
Length: 38min 46sec (2326 seconds)
Published: Fri Mar 24 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.