Bcrypt Error: Illegal String Unidentified Arguments

here is my full code

var express = require('express'), app = express(), mongoose = require('mongoose'), bodyParser = require('body-parser'), morgan = require('morgan'), webToken = require('jsonwebtoken'), bcrypt = require('bcryptjs'), assert = require('assert'); Schema = mongoose.Schema, secretKey = "omjdiuwkslxmshsoepdukslsj"; //User Schema var userSchema = new Schema({ username: {type: String, required: true, index: {unique:true}}, password: {type: String, required: true, select: false} }) userSchema.pre('save', function(next){ var user = this; if(!user.isModified('password')) return next(); bcrypt.hash(user.password, null, null, function(err, hash){ if(err) return next(err); user.password = hash; next(); }) }); userSchema.methods.comparePassword = function(password){ var user = this; bcrypt.compare(password, user.password, function(err, result){ if(err){ console.log(err); } else { console.log("passwords match!"); return; } }) } var userModel = mongoose.model('users', userSchema); //Connecting to Mongo mongoose.connect("mongodb://localhost/userstories", function(err){ if(err) { console.log(err); } else { console.log("Connected to database!"); } }); //Creating Token function createToken(user){ var token = webToken.sign({ _id: user.id, username: user.username }, secretKey,{ expiresIn: 30 * 60 * 1000 }) return token; } //Middlewares app.use(bodyParser.urlencoded({extended: true})); app.use(bodyParser.json()); app.use(morgan('dev')); //Api's app.post('/signup', function(req, res){ var user = new userModel({ username: req.body.username, password: req.body.password }) user.save(function(err){ if(err){ console.log(err); } else{ res.json({message: "User created!"}); } }) }) app.post('/login', function(req, res){ userModel.findOne({username: req.body.username}, function(err, user){ if(err) console.log(err); if(!user){ res.send("User not found!"); } else if(user){ var validPassword = user.comparePassword(req.body.password); if(validPassword){ var tokens = createToken(user); res.json({ success: true, message: "Successfully logged In", token: tokens }); } else { res.send("Invalid password"); } } }) }); //Running the server app.listen(3000, function(err){ if(err) console.log("port not working"); else{ console.log("Everything went just fine"); } }) 

I tried all the approaches and saw all the answers here. But no one is confronted with this illegal argument error. Please draw this for me. I am sure there is a mistake that I do not see.

+7
source share
2 answers

I tried the same authentication code once and got the same Error: Illegal arguments: string, function error.

But I did not see any problems with the code. The fact is that I registered two users with the same username and with a different password. Then, when I tried to log in with a username and one password, this error occurred and stopped the server.

Thus, it seems that you are facing the same problem. Make sure there are no errors with this material if you do not want to have an error in your code.

+1
source share

I also encountered the same error when I used bcrypt.compareSync("input to be compared with the hash", hash) .

Later, I discovered that I had to pass the actual value in the first input parameter , i.e. (the actual value from which the hash was created) and the hash in the second input parameter, but I passed the hashed values ​​in both input parameters.

After correcting this parameter, he gave me the desired result as true or false.

You can also run and check your code here .

+1
source share

All Articles