Data and salt arguments required error (authorization)

204
January 10, 2020, at 7:40 PM

I'm a beginner in NodeJS and I've tried to make an authentication form using NodeJS + express. I want to make a validation for my password (when "confirmpassword" is different than "password" it should return nothing. Unfortunately, I keep getting "data and salt arguments required". I tried in different ways, to put some conditions, but I keep getting this error. Any ideas how I should make it work? Here is the file user.js:

const pool = require('./pool');
const bcrypt = require('bcrypt');

function User() {};
User.prototype = {
find : function(user = null, callback)
{
    if(user) {     
        var field = Number.isInteger(user) ? 'id' : 'username';
    }
    let sql = `SELECT * FROM users WHERE ${field} = ?`;
    pool.query(sql, user, function(err, result) {
        if(err) 
            throw err
        if(result.length) {
            callback(result[0]);
        }else {
            callback(null);
        }
    });
},
create : function(body, callback) 
{
    var pwd = body.password;
    var cpwd = body.confirmpassword;
    // here i hash the pass
    body.password = bcrypt.hashSync(pwd,10);
    body.confirmpassword = bcrypt.hashSync(cpwd, 10);
    if (body.password != body.confirmpassword){
        callback(null);
    }
    else {
    var bind = [];
    for(prop in body){
        bind.push(body[prop]);
    }
    let sql = `INSERT INTO users(username, fullname, password) VALUES (?, ?, ?)`;
    pool.query(sql, bind, function(err, result) {
        if(err) throw err;     
        callback(result.insertId);
    });
    }
},
login : function(username, password, callback)
{
    this.find(username, function(user) {
        if(user) {
            if(bcrypt.compareSync(password, user.password)) {       
                callback(user);
                return;
            }  
        }
        callback(null);
    });    
}
}
module.exports = User;

And the file pages.js:

const express = require('express');
const User = require('../core/user');
const router = express.Router();
const user = new User();
router.get('/', (req, res, next) => {
    let user = req.session.user;
    if(user) {
        res.redirect('/home');
        return;
    }
    res.render('index', {title:"My application"});
})
router.get('/home', (req, res, next) => {
    let user = req.session.user;
    if(user) {
        res.render('home', {opp:req.session.opp, name:user.fullname});
        return;
    }
    res.redirect('/');
});

router.post('/login', (req, res, next) => {
    user.login(req.body.username, req.body.password, function(result) {
        if(result) {           
            req.session.user = result;
            req.session.opp = 1;          
            res.redirect('/home');
        }else {        
            res.send('Username/Password incorrect!');
        }
    })
});
router.post('/register', (req, res, next) => {
    let userInput = {
        username: req.body.username,
        fullname: req.body.fullname,
        password: req.body.password
    };
    user.create(userInput, function(lastId) {
        if(lastId) {
            user.find(lastId, function(result) {
                req.session.user = result;
                req.session.opp = 1;
                res.redirect('/home');
            });
        }else {
            console.log('Error creating a new user ...');
        }
    });
});
router.get('/logout', (req, res, next) => {
    if(req.session.user) {
        req.session.destroy(function() {
            res.redirect('/');
        });
    }
});
module.exports = router;
Answer 1

In userInput, you are not passing confirmpassword property.

let userInput = {
    username: req.body.username,
    fullname: req.body.fullname,
    password: req.body.password
};

In create method, you are accessing it. var cpwd = body.confirmpassword;

cpwd is null, and that's the reason for the error.

   body.confirmpassword = bcrypt.hashSync(cpwd, 10);//**cpwd is null**

As per the docs, data is required argument and this cannot be null.

hashSync(data, salt)

data - [REQUIRED] - the data to be encrypted.

salt - [REQUIRED] - the salt to be used to hash the password.

READ ALSO
Configuring express gateway to work with redis

Configuring express gateway to work with redis

I'm setting up an instance of the express gateway for routing requests to microservicesIt works as expected, but I get the following errors when I try to include redis in my system config

93
Heroku fetching api data : Error 414 URI too long

Heroku fetching api data : Error 414 URI too long

I have a nodejs server deployed on heorku (hosting provider) and i'm receiving a 414 URI Too Long error

77
Biometric push in Nodejs

Biometric push in Nodejs

In documentation of Biometric, initialization of information exchangeTelling

103