// Hello coders , follow previous post for product architecture - Today we are going to see how to reset password & update it in mysql database-
============== follow previous post for helper files & folders ====
//================= app.js ==============
//to Generate OTP -
var otp;
function setValue()
{
otp = Math.random();
otp = otp * 1000000;
otp = parseInt(otp);
console.log(otp);
}
function getValue() {
console.log(otp); // yup, it's "test"
}
//======= Forgot Button on Login Page- will redirect to /forgotPassord
app.post("/resetpasswordOTP",(req,res)=>{
    //res.redirect("/forgotpassword");            
    res.status(200).json({success:"Enter verified email"}); 
})
//============== Forgot Password Button End ========================
//=============== Page to send OTP method- post, action-"/resetpassword" ============
app.post("/forgotpassword",(req,res)=>{
    const {email}=req.body;
    setValue(); 
        //SendMail() // here we can send otp to email or mobile number with the help of nodemailer but //not using right now instead of we will see otp on terminal or cmd  where our project is running 
        connection.query('SELECT * FROM register WHERE email = ?', [email], (err, rows) => {
                if (err)   
                    res.send("Invalid Accout");                     
                else 
                res.json("OTP Sent"); 
        });    
})
//============== Reset Password with the OTP sent from previous post Method
app.put('/resetpassword',function(req,res){
        const {email}=req.body;
 if(req.body.otp==otp)
   {     
    req.body.cnpass = hashPassword(req.body.cnpass);
        connection.query(`UPDATE register SET password =?  WHERE email= '${email}'`,[req.body.cnpass],(err,result)=>{
                if(err) 
                res.json("Failed to reset password");
                else 
                res.json("Password Reset successfull");
        }); 
   }
   else
   {
       res.json("Wrong OTP");
   }
}); 
=================== end 
Now Test this api on postman Rest client - 
open postman  & test all three api one bye one - 
1.  Method - Post -  url - localhost:3000/resetpasswordOTP 
2. Method Post -  url -localhost:3000/forgotpassword   //===== to send OTP  
================SEE Otp on console 
here OTP is - 886369
3. Method PUT (because we are updating something but post will do also ) - url - localhost:3000/resetpassword
on postmant enter right otp  - 

Comments
Post a Comment