#113 Sending Email for Password Reset | Authentication & Authorization | A Complete NODE JS Course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in the last lecture we started working on the password reset functionality and we implemented few of the Logics like getting the user from the database based on user's email and then generating a password reset token for that user now we need to send that password reset token to the user's email so that he can use it for resetting his password so here we are finding the user based on the email which the user has provided for resetting the password and then we are generating a password reset token here now we want to send this password reset token to the user's email now to work with email that means in order to send email we are going to install a package called node mailer so let's first go ahead and let's do that so let's go to this command terminal here and here let's type this npm install command and we want to install node Miller so the node Miller package has been installed now in order to send email we are going to create an email Handler function which we can reuse in our application whenever required so inside this utils folder let me go ahead and let me create a new file and I will simply call it as email.js and inside this file the first thing which we are going to do is we are going to import node Miller and now let's go ahead and let's create a function let's call it send email and for this function we are going to receive some options basically from which mail we want to send the email to which email address we want to send the email the email message body and all those things so we will set those things on this option and then let's also go ahead and let's export this send email function from here so for that we can say module dot exports basically we want to export this function as default so from here we want to export this send email function all right so let's start implementing the functionality for sending the email so the first thing here which we need to do is we need to create a transporter so the transporter here is basically a service that will actually send the email because it's not the node.js that will send the email the transporter will be a service which we need to Define here and then it will be responsible for sending the email for example a service like Gmail but Gmail is not the service which we are going to use here because here since we are trying to create a real world application we are not going to use Gmail service for sending the email and also apart from Gmail we also have other well-known services like send grid and mail gun Etc we will use send grid a bit later in this course for now we will use a spatial development service which basically fakes to send email to real addresses but in reality these emails end up trapped in the development inbox and we can then analyze them by having a look on how they will look in the production and if you want to modify something there we can modify the mail content according to our need so the service which we are going to use here is called as mail trap and in order to use it we need to sign up for that so let's open browser and there let's search for mail track and let's open it so this is the service which we are going to use basically with this service we can fake to send emails to the client but these emails will never reach these clients in actual and instead it will be trapped in our mail trap and in this way it avoids sending any development emails to your client or to your application users so let's go ahead and let's sign up for this service here I am going to sign up using my Google account so I'm signed up here now let's click on the start testing button and currently here we have one inbox which is called as my inbox and it is this inbox where we will receive all the mails so let's again go to this my inbox and here we can see the details of all the configurations which we need for example The Host the port number which we can use the username and the password which we need in order to send the email so let's go back to vs code and there let's do this configuration now I'm going to save these configurations in config.in file so there let's just go ahead and let's first create some environment variables for example email underscore user and to this we will save the user ID which we need in order to send the email so I will copy this username and paste it here then let's also go ahead and let's create another environment variable email password and again let's go back to the browser from there let's copy this password and let's assign it to this email password then we are going to create another environment variable email underscore host and there we are going to assign this host and finally let's also create email underscore Port so the port number which we will use for sending the email and for that we can select any of these ports okay so let me select the first one here and let's specify it here and let's save this file all right now let's go back to our email.js and there let's go ahead and let's create a transporter so here I will create a variable I will call it transporter and in order to create the transporter we can say node Miller Dot create transport and to this we need to pass the options so the first option which we need to set here is service and to this service we can assign any service for example if you want to use Gmail service you can assign Gmail here but here we want to use our host in this case since we are going to use this mail trap so in this case the host is this one okay and we are assigning this value in our environment variable to this email host so let's go ahead and let's specify this email host here to this service for that we can say process dot in dot email host then we also need to specify the port again we are going to use the environment variable so it is process dot in Dot email port and then we need to specify the auth property and this is going to be an object and in here we need to specify the user so in this case the user is going to be email underscore user and actually it is an environment variable so here we need to say process dot in dot email user and then let's also specify the password Here and again we are going to use the environment variable so let's say process dot in dot email underscore password okay so this is our configuration for transport in node mailer now depending on the service which you are using this configuration might change for example if you use Gmail here then the configuration will look a bit different moving on the next step is to Define some options for the email we are going to send so let's create another variable let's call it email options and it is going to be an object and in here we have to Define some options so first we need to specify from which email we want to send the email so it is going to be a string and here let's simply say cineflix support so this is the name and then we also need to specify the email address so let's simply say support at cineflix.com okay then we need to specify the two option so this two option basically the mail to which we want to send this email that will come from this options parameter okay so here we will simply say option dot email then we also need to specify the subject again this subject will also come from this option so we can say option Dot subject and we also need to specify the email text okay and again this is also going to come from this option so we can say option Dot and on this option we will set a message property so we want to read that message and we want to set it as email text finally let's go ahead and let's send this email for that on the transporter so basically this variable which we are creating here we can call a function called send mail and to this send mail we need to pass this email options okay let's save this file so now when we will call this send email function this function is responsible for sending an email so let's go ahead and let's use this function in our auth controller at this line now in order to use that send email function first we need to import this email.js in this file so let's scroll up and let's require the email module so let's create a variable let's call it send email that is the function name and we want to require the email module so for that first we need to move one folder up in there we have this utils folder and in there we have this email module okay and now let's go ahead and let's call this send email so scroll down and here we want to call that send email function also keep in mind that this send mail function here it is an asynchronous function so basically it is going to return a promise and we need to wait for that promise to get resolved for that we can use this away keyword here and since we are using this await keyword here we need to make this function as YNC okay and then when we are calling this send email function here here also we need to use the await keyword now when we are calling this send email function we also need to pass some options right so what we are going to do is first I will create a variable I will call it reset URL so basically we want to send this reset URL in the email body so that user can click on that reset URL and reset his password here I will use back text and let's go ahead and let's create this reset URL so first we need to specify the protocol for this URL it is going to be either HTTP or https so we are going to read that protocol from the request so here I will use this template little syntax and there we can say request dot protocol okay so this protocol is either going to be HTTP or https after that let's use colon double slash and then we want to get the domain basically the root URL so again we can get that from the request so here again we can say request dot get and we want to get the host so in this case in our example this expression here it is going to return the localhost basically it is going to return the IP address 127.0.0.1 colon the port number which is 3000 in our case okay after that we have slash API slash V1 slash users and then slash and now here we are going to specify the reset password so it is basically going to be this URL to which the user will send a reset password request right so if I go back to this auth router.js you see here we are creating that route slash reset password so after this URL after this URL we are going to specify reset password in order to send a reset password request and here in this route this here it is not going to be a post request it is going to be a patch request because here we are not going to create any new resource here we are going to update an existing resource right basically here we are going to update the password of an existing user so here it should be patch the request method should be patch and with this reset password we are also going to specify a route parameter and let's simply call it as token so this token here is basically going to be the reset password token so this token is basically going to be the password reset token which we are sending to the user in the email okay so when the user will make a password reset request in the URL he will also specify that reset token so here after this let's say slash and we are going to have that reset token inside this reset token variable so let's specify it here okay then let's go ahead and let's create the email text so for that I will create this message variable here and the text let's say is going to look something like this let's say it will say we have received a password reset request please use the below link to reset your password then we want to give a line break maybe two line breaks and then we want to specify the reset password link so again I will use the template digital syntax here and we are storing that reset password link inside this reset URL so let's specify it here okay again let's add two line breaks and then let's say this reset password link will be valid only for 10 minutes okay now to this send email let's pass some options so here let's specify the email to which we want to send this email and we are going to get it from the user object so we can say user Dot email then let's also specify the subject and for the subject let's say password change request received or you can specify any subject here okay and then let's also specify the message and to this message we are going to assign this message variable okay now when we are using this send email function and since it is running asynchronously it might be possible that it returns a rejected promise and this can happen due to several reasons for example the service which we are using for sending the email is not running or because of some other reason so here we might have a rejected promise but we are not going to handle that rejected promise using our Global error handling middleware that's because if the mail is not sent with the password reset token to the user we might want to reset the password reset token and password reset expired token in the database to undefined because if you don't do that then the email is also not sent and in the database the password reset token and the password reset token expired at is still set with some value and we don't want that so we are going to handle the rejected promise returned by the send email here itself using the try catch block so let me go ahead and let me use a try catch block and inside this try block let's put this code and let's also specify the catch Block in this cash block we will receive the error that has occurred so if the reset password email is not sent that means if this function has returned some rejected promise in that case what we are going to do is on the user object we are going to set the password reset token to undefined okay in the same way we will also set password reset token expires to undefined and then we want to save these two values in the database so for that on the user object we are going to call the save function and there we don't want to perform the validation so we will use validate before save and we will set it to false because here we don't want the validation and then we want to call the global error handling middleware so for that from here we can simply return by calling the next function and in there let's pass an instance of our custom error class and in the error message let's say there was an error sending password reset email please try again later okay and here the status code will be 500 because this type of error might occur due to some server issue so that's why we are specifying this 500 that means internal server error so this code will be executed if this send email is not able to send a password reset email to the user that means if it has returned a rejected promise but if this function has send the email properly that means if the user has received a password reset email in that case let's send a simple response so for that let's say response Dot status so status here I'm going to set to 200 that means everything is okay and in the response we are going to send some Json data and in that Json data let's specify the status as success and let's set a message and there let's say password reset link sent to the user email okay with this let's try to test this functionality so let's save the changes here let's go to this terminal so the server has started let's wait for DB connection to be successful and the DB connection is successful let's go to postman and here we are in the forgot password request let me actually save this request first and I will save it in user folder and let's call it as forgot password okay let's save this request all right now let's go ahead and let's make a request to this forgot password so you see we are receiving this error message there was an error sending password reset email please try again later it looks like there is something wrong let's go to vs code and let's go to this email.js all right uh so in node mailer there have been some changes so now instead of service I think we need to use host and instead of password we need to use simply pass okay let's see if the change is now and let's try it out now so let me go back to postman and now let's try to make a request to this forward password so when I click on the send button all right so now we have this success response and it says password reset link sent to the user email so now we can go back to the browser and now you will notice that here we have a mail and if I click on this mail this is the body of that Mill okay so you can see from cineflex support to Market gmail.com but in actual this mail has not been sent to this email address it is just trapped here in this mail trap and this is the request body which we have specified and in this request body you will see the password reset link okay so it is HTTP localhost Port 3000 API V1 slash user slash reset password and after that we have the password reset token and we can verify this reset password token if it is same or not for that we can go to vs code and there we are logging the plain password reset token and the encrypted password reset token and we are sending the plain password reset token in the email right so as you can see it ends with e0189 and if I go back to the mail there also it ends with e0189 so it is the same password reset token so now this forgot password functionality it is working as expected we are sending the email to the user with the password reset link so now in the next lecture we are going to implement the reset password functionality where the user has to make a request to this URL okay basically he will make a request to this URL to this reset password and in the URL only we are going to send this password reset token and based on that we are going to allow user to reset his password so this is all from this lecture just keep in mind that now with the node Miller instead of service we need to use host and instead of password we need to use pass all right so this is all from this lecture if you have any questions then feel free to ask it thank you for listening and have a great day
Info
Channel: procademy
Views: 9,569
Rating: undefined out of 5
Keywords: authentication, authorization, express, mongoose, mongodb, node js, node js tutorial, complete node js course
Id: tRM6IJfuFxA
Channel Id: undefined
Length: 23min 15sec (1395 seconds)
Published: Thu May 18 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.