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

Unhandled "error" event. (Incorrect arguments) #158

Open
syedmkazmi opened this issue Aug 24, 2017 · 2 comments
Open

Unhandled "error" event. (Incorrect arguments) #158

syedmkazmi opened this issue Aug 24, 2017 · 2 comments

Comments

@syedmkazmi
Copy link

Im using passport.js (local strategy) to authenticate users but I am getting the following error:

Unhandled "error" event. (Incorrect arguments)

screen shot 2017-08-24 at 15 21 33

index.js file:

const {register: registerUser, login: loginUser} = require('../controllers/authentication');

// login
router
.route('/login')
.post(loginUser);

Authentication.js file:

// login function
let login = (req,res) => {

if(!req.body.email || !req.body.password){
    sendJsonResponse(res, 400, {"message": "All fields required"});
    return;
}

passport.authenticate('local', (err, user, info) => {

    let token;

    if(err){
        sendJsonResponse(res, 404, err);
        return;
    }

    if(user){
        token = user.generateJwt();
        sendJsonResponse(res, 200, {
           "token": token
        });
    } else {
        sendJsonResponse(res, 401, info);
    }
})(req,res);
};

Passport.js File:

const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const mongoose = require('mongoose');
const User = mongoose.model('Users');

passport.use(new LocalStrategy({
    usernameField: 'email',

}, function (email, password, done) {
    User.findOne({'email': email}, function(err, user) {
        if(err){
            return done(err);
        }

        if(!user){
            return done(null, false, {message: 'Incorrect Username.'});
        }

        if(!user.validPassword(password)){
            return done(null, false, {message: 'Incorrect Password.'});
        }
        return done(null, user)
    });
   }
));

I can't seem to figure out what the issue might be. I've tried checking typeof for both email and password and they are indeed Strings. Does anybody know what could be the issue.

@CanGokdere
Copy link

CanGokdere commented Oct 14, 2017

Hello @syedmkazmi , I had the same problem and found the cause. My user model did not have to have a valid password (if users logged in with social accounts) so the problem was at validPassword method of user schema. In order for bcrypt compare to work, you have to have a bcrypt hashed string password.

userSchema.methods.validPassword = function(candidatePassword) {
    if(this.password != null) {
        return bcrypt.compareSync(candidatePassword, this.password);
    } else {
        return false;
    }
};

You get the gist. Hope it helps.

@nelsonfleig
Copy link

You are a god @CanGokdere . I overlooked this before moving to production and suddenly the app started crashing and without much information in the log regarding the issue. Thank you so much for saving my butt here!

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