Simple Node Auth Backend #11 - OTP Forgot & Reset Password I

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
up to this point in a node.js authentication backend we can sign up login and verify emails successfully using an OTP system now let's see how we can safely reset forgotten passwords based on the OTP system we created earlier to start this we need a new domain for forgotten passwords so we create a new directory for that we then create a file for handling the routes here we import Express and initialize and express router the first route to handle is the one to be used for requesting a password reset so that's the forgot password this will be a post request to the root path to this we pass an async routes Handler which takes the request and response parameters now to trigger the forgot password we need the user email address so we distracted that from the body of the request we then check if it's empty if that's true we threw an arrow with a message otherwise you have a good email so we can't proceed to proceed we'll do that in a controller file here we create a simple listing function that will be used to send a password reset email to the user this function will expect to receive the email of the user now to start we check to see if a user with a provided email exists in our system to do this we import the exam model now we check for the existing user by passing the email address if there is no record we throw an error with an appropriate message also you might want to limit the password reset to only verified users if that's the case we check for the verified property of the fetched user if the user isn't verified already we throw an error with the message at this point we create and send the one-time pin to the user so we have to prepare the details for it the OTP system equated expects an email as subject a message and an optional duration now we import the send.tp function from the OTP domain we then passed the details to create and send the OTP to the user after this we return the created OTP in the case of any error we throw it and at the bottom we export the function now back in the routes file we import the function we just created to send the email we then call it with the email to send the OTP to the user which also Returns the created OTP now we respond to a successful request by passing the created OTP in the case of an error we send an error response with a received message and at the bottom we export the router now to be able to test this part we expose the route to the server app we start this by creating an index file in the forgot password domain in this file we Import and Export the routes next we visit the index file in the main route directory here we import the forgot password routes we then assign the forgot password path to the imported routes at this point we can test so with the server still running in the background we open Postman and create a post request here we Supply the path to request for a partial reset we then set the body of the request to Json and pass our email address to get new errors this should be already in the database and should be verified if all goes well we should receive the new OTP record also checking our inbox we should see a new email about the request with an OTP this sums up the first part next we'll work on verifying the OTP and updating the user password now we can save and commit all the changes in our code and proceed in the next part link to the full source code will be in the description
Info
Channel: ToThePointCode
Views: 4,212
Rating: undefined out of 5
Keywords: tothepointcode, node js password reset, node js forgot password api
Id: F_3o5PhqGwo
Channel Id: undefined
Length: 4min 38sec (278 seconds)
Published: Mon Feb 06 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.