node.js

Create a Login Form With Node.js & MySQL

In our previous example, we saw how we can create a registration form using Node.js and MySQL.

Using that same setup, let us proceed to authenticate the user after he has completed his registration.

First, we create an HTML form for the user to enter his login credentials.

We create a login.html file

<html>  
<body>  
 
<form action="authenticate-controller" method="POST">  
 
Email: <input type="text" name="email">
Password: <input type="password" name="password">
   
<input type="submit" value="Submit">  
</form>
 
   
</body>  
</html> 

Next, we create a script that will authenticate the user. Let us create a file called authenticate-controller.js.

Read Node.js documentation

var Cryptr = require('cryptr');
cryptr = new Cryptr('myTotalySecretKey');
 
var connection = require('./config');
module.exports.authenticate=function(req,res){
    var email=req.body.email;
    var password=req.body.password;
   
   
    connection.query('SELECT * FROM users WHERE email = ?',[email], function (error, results, fields) {
      if (error) {
          res.json({
            status:false,
            message:'there are some error with query'
            })
      }else{
       
        if(results.length >0){
  let decryptedString = cryptr.decrypt(results[0].password);
            if(decryptedString){
                res.json({
                    status:true,
                    message:'successfully authenticated'
                })
            }else{
                res.json({
                  status:false,
                  message:"Email and password does not match"
                 });
            }
          
        }
        else{
          res.json({
              status:false,    
            message:"Email does not exits"
          });
        }
      }
    });
}

Finally we test our authentication script and see the results.

Author

View Comments

Recent Posts

Apple is developing a doorbell camera equipped with Face ID technology.

Apple is reportedly developing a new smart doorbell camera with Face ID technology to unlock…

3 hours ago

Google Launches Its Own ‘Reasoning’ AI Model to Compete with OpenAI

This month has been packed for Google as it ramps up efforts to outshine OpenAI…

2 days ago

You can now use your phone line to call ChatGPT when cellular data is unavailable.

OpenAI has been rolling out a series of exciting updates and features for ChatGPT, and…

4 days ago

Phishers use fake Google Calendar invites to target victims

A financially motivated phishing campaign has targeted around 300 organizations, with over 4,000 spoofed emails…

4 days ago

Hackers Exploiting Microsoft Teams to Remotely Access Users’ Systems

Hackers are exploiting Microsoft Teams to deceive users into installing remote access tools, granting attackers…

5 days ago

Ethical Hacking Essentials

Data plays an essential role in our lives.  We each consume and produce huge amounts…

7 days ago