user8331511
user8331511

Reputation:

if(err) throw err; Error: Illegal arguments: string, undefined

I now have

module.exports.comparePassword = function(candidatePassword, hash, callback) {
  console.log(candidatePassword)
 console.log(hash)
  bcrypt.compare(candidatePassword, hash, function(err, ismatch) {
    if(err) throw err;
    callback(null, ismatch);
  });
}

and the logs now are

Server is up on port 3000
Connection has been established
Sat Jan 13 2018 14:45:36 GMT+0000 (GMT): GET /users/login
Sat Jan 13 2018 14:45:42 GMT+0000 (GMT): POST /users/login
testing1234
undefined
/Users/benbagley/Code/poetry-out-loud/models/user.js:101
    if(err) throw err;
            ^

Error: Illegal arguments: string, undefined
    at _async (/Users/benbagley/Code/poetry-out-loud/node_modules/bcryptjs/dist/bcrypt.js:286:46)
    at Object.bcrypt.compare (/Users/benbagley/Code/poetry-out-loud/node_modules/bcryptjs/dist/bcrypt.js:304:13)
    at Function.module.exports.comparePassword (/Users/benbagley/Code/poetry-out-loud/models/user.js:100:10)
    at /Users/benbagley/Code/poetry-out-loud/routes/users.js:176:12
    at model.Query.<anonymous> (/Users/benbagley/Code/poetry-out-loud/node_modules/mongoose/lib/model.js:4056:16)
    at /Users/benbagley/Code/poetry-out-loud/node_modules/kareem/index.js:273:21
    at /Users/benbagley/Code/poetry-out-loud/node_modules/kareem/index.js:131:16
    at process._tickCallback (internal/process/next_tick.js:150:11)

It seems here the password is getting shown but not the hash.

Original

Hi I'm getting the following error, not sure what's causing it

Message sent: <[email protected]>
Preview URL: https://ethereal.email/message/WlVWjq0qIgpSmhJbWloWhUGTHAp3fWC4AAAAbOQTYPu-4HjQWkI0i1uv5Ds
Sat Jan 13 2018 14:24:05 GMT+0000 (GMT): GET /users/login
Sat Jan 13 2018 14:24:24 GMT+0000 (GMT): POST /users/login
/Users/benbagley/Code/poetry-out-loud/models/user.js:99
    if(err) throw err;
            ^

Error: Illegal arguments: string, undefined
    at _async (/Users/benbagley/Code/poetry-out-loud/node_modules/bcryptjs/dist/bcrypt.js:286:46)
    at Object.bcrypt.compare (/Users/benbagley/Code/poetry-out-loud/node_modules/bcryptjs/dist/bcrypt.js:304:13)
    at Function.module.exports.comparePassword (/Users/benbagley/Code/poetry-out-loud/models/user.js:98:10)
    at /Users/benbagley/Code/poetry-out-loud/routes/users.js:176:12
    at model.Query.<anonymous> (/Users/benbagley/Code/poetry-out-loud/node_modules/mongoose/lib/model.js:4056:16)
    at /Users/benbagley/Code/poetry-out-loud/node_modules/kareem/index.js:273:21
    at /Users/benbagley/Code/poetry-out-loud/node_modules/kareem/index.js:131:16

The users are being created I just can't sign in.

Here is the lines causing the error

module.exports.comparePassword = function(candidatePassword, hash, callback) {
  bcrypt.compare(candidatePassword, hash, function(err, ismatch) {
    if(err) throw err;
    callback(null, ismatch);
  });
}

here is the passport implementation

passport.use(new LocalStrategy({
  usernameField: 'email'
  },
  function(email, password, done) {
    User.getUserByEmail(email, function(err, user){
      if(err) throw err;
      if(!user){
        return done(null, false, {message: 'Unknown Email Address'});
      }

      User.comparePassword(password, user.password, function(err, ismatch){
        if(err) throw err;
        if(ismatch){
          return done(null, user);
        } else {
          return done(null, false, {message: 'Invalid password'});
        }
      });
    });
  }));

Upvotes: 6

Views: 13825

Answers (11)

Mohit Saud
Mohit Saud

Reputation: 115

  • I think you are missing the password field in the user or the password is null.
  • console.log(user)

The solution which worked for me is:

if(!user.password){
 throw new Error('Invalid Credentials')
}

Upvotes: 0

const comparePassword = async(candidatePassword, hash) => {
  const comparedPassword = await bcrypt.compare(candidatePassword, hash);

  if (comparedPassword)
    return comparedPassword;
}

Upvotes: 0

KUYESO ROGERS
KUYESO ROGERS

Reputation: 1

Try to check the schema. In my case, I had select: true. When I commented it out, it worked fine.

password: {
    type: String,
    required: [true, 'Please add a password'],
    minlength: 6,
    // select: false
    trim: true,
},

Upvotes: 0

DanielAzulay
DanielAzulay

Reputation: 1

I had the same problem was the header on the insomina, it was passing the password undefined it was like that insomnia before i change to this: insomnia after

and solved my problem

Upvotes: 0

oxk4r
oxk4r

Reputation: 517

I dealed with this problem in the past using MariaDB and the culprit was the table column PASSWORD that was in uppercase. When I changed it to lowercase, everything worked correctly.

Upvotes: 0

Shubham Saurav
Shubham Saurav

Reputation: 41

You might have set the select property to false on password. And this is causing the error. The mongoose is not returning the password because you have set the select to false. You now need to explicitly ask for the password when querying. Use the select method where you are querying for the user and pass the +password as a string like this.

.select('+password')

Upvotes: 1

Dolapo Johnson
Dolapo Johnson

Reputation: 113

Error: Illegal arguments: string, undefined could mean either your hash value or user input is not recognized a string by the compiler. First try to log your input together with what your user(from database is returning), if your user.password does not return a string then you should check your Schema if it was predefined a string and make sure you don't use select: false in your password, it may restrict query.

Upvotes: 1

lazycipher
lazycipher

Reputation: 350

In my case the error was -

E:\web\Projects\webapp\auth.js:15
                if(err) throw err;
                        ^

Error: Illegal arguments: number, string
    at _async (E:\web\Projects\webapp\node_modules\bcryptjs\dist\bcrypt.js:286:46)
    at Object.bcrypt.compare (E:\web\Projects\webapp\node_modules\bcryptjs\dist\bcrypt.js:304:13)
    at Promise (E:\web\Projects\webapp\auth.js:11:20)
    at process._tickCallback (internal/process/next_tick.js:68:7)
[nodemon] app crashed - waiting for file changes before starting...

It was because the password was in number and it was throwing an error! I just converted the entered password into string!

Code Before Solving Error-

bcrypt.compare(password.toString(), user.password, (err, isMatch)=>{
                if(err) throw err;
                if(isMatch){
                    resolve(user);
                }else{
                    //Password Wrong
                    reject("Auth Failed");
                }
            });

After Solving Error- What I did is, I added .toString() function in the password argument to convert it into string!

bcrypt.compare(password.toString(), user.password, (err, isMatch)=>{
                if(err) throw err;
                if(isMatch){
                    resolve(user);
                }else{
                    //Password Wrong
                    reject("Auth Failed");
                }
            });

I hope it helps someone.

Upvotes: 3

oyeyipo
oyeyipo

Reputation: 409

Had the same issue.

Add console.log() for the passed in values shows they are "undefined".

I tested the endpoint out using Postman to add values to the required field(s), solves the problem.

conclusion: the Password is probably empty. Check if it undefined using console.log(). Then pass some value to it.

Upvotes: 3

Marcos Porto
Marcos Porto

Reputation: 19

Don't forget to use toObject():

var pass = user.toObject().userPass;

Upvotes: -1

Nikola Andreev
Nikola Andreev

Reputation: 634

Try to console .log() some values to ensure everything is defined well.

module.exports.comparePassword = function(candidatePassword, hash, callback) {
  console.log(candidatePassword)
  console.log(hash)
  bcrypt.compare(candidatePassword, hash, function(err, ismatch) {
    if(err) throw err;
    callback(null, ismatch);
  });
}

Upvotes: 0

Related Questions