Skip to main content

Forgot Password API - with Postman Testing node js , express js , MySQL,

 // 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 -


Password has been changed you can see your MYSQL Database



Comments

Popular posts from this blog

How to deploy NodeJS app on server with Apache2 and acess it with Server IP addresss - Node JS deploy project Live.

     hello friens , This is Vasu Birla , in previous post we have seen the deployement of NodeJS app on AWS instance server instance but AWS server is expensive than other servers . SO today we will use simple Ubuntu server for making Live out Project using Apache2 . At the end you will be abe to access your NodeJS app using server IP address from anywhere  .. 1.  Login to your server using SSH terminal . (in AWS part i already explained how to do this )     -> Open your putty in hostname put your server IP -      login with ssh username ->root and password     (if you dont have root username and pass ask your      provider or reset it from cpanel or hosting panel ) 2. after login - on terminal you can Put your project anywhere.     There are two ways to put your project folder on server location     (i) - > using Github - (very popular and easy to track your everyday code changes to your project folder)             you can push your entire project folder to git-hub and and

Part 19- Router (Networking Devices)- Computer Networking- CCNA

Hello friends...i am Vasu Birla and today will discuss about the most important Networking Device ..Router.  so let's start... ROUTER Router is a device which connect two or more networks together, which is why router is also known is Inter-networking device also. Inter-networking means two or more networks are connected together with the help of router. one more thing router is just like a computer  but it is designed for routing only, our computer can be router also but that are software router while hardware router which are specialize for routing is more efficient and fast than software router.  There is a Operating System installed on router which get moves data from one network to another network with the help of routing table.  Router does work on Network layer or Layer 3 of the OSI model.  Cisco Router There many companies which manufacture Router but main companies are - Cisco , Juniper , HP, 3com and Nortel  Functions of Router

Part 16- HUB (Networking Devices)- Computer Networking- CCNA

hello friends , Today we will discuss about the second Networking Devices Ethernet HUB. HUB Hub is a centralize device via all devices (computers, printers etc.) are connected with the star topology.  Hub connects other multiple devices together and helps to make connectivity mutually so that they can share and transfer data to each and every devices which is connected to that hub.  Hub does work at the first layer means at Physical layer of the OSI Model. Hub has many ports, and each port works like a repeater so Hub is also known as Multiport Repeater. A Hub Types of Hubs. there are three kind of Hubs. Active , passive and Manageable hubs. 1. Active Hubs - These hubs regenerates and amplify the signals to keep them strong. one more thing there is electricity needed in the active Hubs. 2. Passive Hubs- Passive Hubs simply forward the received signals. this type of hub does not make pure signal or regenerate so  these passive hubs doesn't n