Wednesday, 10 May 2017

nodejs login form using mysql and html

I get this simple code from a tutorial to a sample login html form to detect if user and the password are on my database for a user register or not ?

this code can detect the email if exist but the password no so what's the wrong ?

var express = require('express');
var app = express();
var  server = require('http').createServer(app);
bodyParser = require('body-parser');
var mysql = require('mysql');
var connection = mysql.createConnection({
      host: 'localhost',
      database: 'chmult',
      user: 'root',
      password: '',
    });
users = [];
connections = [];


app.get('/', function(req, res){
    res.sendFile(__dirname + '/');

});



app.use(bodyParser.urlencoded({
    extended: true
}));

/**bodyParser.json(options)
 * Parses the text as JSON and exposes the resulting object on req.body.
 */
app.use(bodyParser.json());
connection.connect();


app.post('/', function(req, res){




var username= req.body.user.username;
var password = req.body.user.password;
connection.query('SELECT * FROM tesko WHERE username = ?',[username], function (error, results, fields) {
if (error) {
  // console.log("error ocurred",error);
  res.send({
    "code":400,
    "failed":"error ocurred"
  })
}else{
  // console.log('The solution is: ', results);
  if(results.length >0){
    if([0].password == password){
      res.send({
        "code":200,
        "success":"login sucessfull"
          });
    }
    else{
      res.send({
        "code":204,
        "success":"Email and password does not match"
          });
    }
  }
  else{
    res.send({
      "code":204,
      "success":"Email does not exits"
        });
  }
}
});




});




app.listen(3231);
console.log('Example app listening at port:3231');

my html forms

<form method="post" action="">
    <input type="text" name="user[username]">
    <input type="text" name="user[password]">

    <input type="submit" value="Submit">
</form>
</html>

my columns on my tables names are (username,password) the both are varchar and I tried with other table that have md5 but also can't detect the password



via dark night

No comments:

Post a Comment