Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maybe a CompareSync Bug #68

Open
jackalsin opened this issue Aug 10, 2016 · 6 comments
Open

Maybe a CompareSync Bug #68

jackalsin opened this issue Aug 10, 2016 · 6 comments

Comments

@jackalsin
Copy link

I encounter an issue when using CompareSync in model

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var bcrypt = require('bcrypt-nodejs');

// create a schema
var userSchema = new Schema({
  username: { type: String, required: true, unique: true },
  password: {type: String, required:true}
});

userSchema.methods.generateHash = function(password) {
  return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
};

// checking if password is valid
userSchema.methods.validPassword = function(password) {
  console.log("Normal call 7 " + password + "\t" + this.password); //todo debug line
  var result = bcrypt.compareSync(password, this.password);
  console.log("Normal 8" + result);
  return bcrypt.compareSync(password, this.password);
};

var Users = mongoose.model('Users', userSchema);
module.exports = Users;

The result is as follows

Normal call 7 password1234      password1234

events.js:74
        throw TypeError('Uncaught, unspecified "error" event.');
              ^
TypeError: Uncaught, unspecified "error" event.
    at TypeError (<anonymous>)
    at Function.EventEmitter.emit (events.js:74:15)
    at Query.<anonymous> (<project dir>/node_modules/mongoose/lib/model.js:3326:13)
    at <project dir>/node_modules/mongoose/node_modules/kareem/index.js:259:21
    at <project dir>/node_modules/mongoose/node_modules/kareem/index.js:127:16
    at process._tickCallback (node.js:415:13)

As you can see, the program is normal before calling compareSync, and the Strings to compare are quite reasonable, and it throws an exception

@xgui3783
Copy link

I suspect the error occurs because compareSync(string,hash) takes string as the first argument and hash as the second. I suspect since hash should have considerably longer (and fixed) length, that's where the error occurs.

@jackalsin
Copy link
Author

Thanks for the reply. After 4 days, I found it's a mongoose issue... Sorry for this. You can close now

Related link

@skulabs
Copy link

skulabs commented Oct 6, 2016

@jackalsin I read that issue and couldn't figure out what the verdict was. From what I see, bcrypt.hashSync returns a string.

var query = {
    'users._id': user_id
};

var update = {
    '$set': {
        'users.$.password': str
    }
};

Why would this be an issue? Can you please explain what you did to resolve the issue?

var bcrypt = require('bcrypt-nodejs');

var salt = bcrypt.genSaltSync();

var hash = bcrypt.hashSync('foofoo', salt);

console.log(typeof hash);
console.log(hash);
string
$2a$10$L0YOhVWpAtDZg0nBnR9EsOKnVIt/eEzGwnlpaA1Ok0DDfaU6H4EV6

@darmie
Copy link

darmie commented Dec 5, 2016

I am having the same issue... how is hash not a string?

@jackalsin
Copy link
Author

You can console.dir(the_obj_you_want_hash). Mongoose has changed the api. Dir the object.

@darmie
Copy link

darmie commented Dec 5, 2016

Fixed. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants