Friday, 28 April 2017

Passportjs & mysql (Local-Strategy) - Error: Cannot enqueue Query after invoking quit

I've some difficulties to integrate mysql into a Passport-local.

I tried this :

    passport.use(
    'local-signup',
    new LocalStrategy({
        // by default, local strategy uses username and password, we will override with email
        usernameField : 'email',
        passwordField : 'password',
        passReqToCallback : true // allows us to pass back the entire request to the callback
    },
    function(req, email, password, done) {
        // find a user whose email is the same as the forms email
        // we are checking to see if the user trying to login already exists
        connection.query("SELECT * FROM user WHERE email = '" + email+"'", function(err, rows) {
            console.log(rows);
            if (err)
                return done(err);
            if (rows.length) {
                return done(null, false, req.flash('signupMessage', 'That email address is already taken.'));
            } else {
                var pass1 = password.toString();
                var pass2 = req.body.passInputBis.toString();
                if(pass1 != pass2) {
                    return done(null, false, req.flash('signupMessage', 'Passwords do not matche'));
                }
                if(password.length < 8) {
                    return done(null, false, req.flash('signupMessage', 'Password to small'));
                }
                // if there is no user with that username
                // create the user
                var newUserMysql = {
                    id: unique_id('user', 8),
                    firstname: req.body.fNameInput,
                    lastname: req.body.lNameInput,
                    username: req.body.username,
                    email: email,
                    password: bcrypt.hashSync(password, null, null)  // use the generateHash function in our user model
                };

                var insertQuery = "INSERT INTO user (id, firstname, lastname, email, username, password ) values ('" + newUserMysql.id +"','"+ newUserMysql.firstname +"','"+ newUserMysql.lastname +"','"+ newUserMysql.email +"','"+ newUserMysql.username +"','"+ newUserMysql.password +"')";

                connection.query(insertQuery, function(err, rows) {
                    return done(null, newUserMysql);
                });
            }

        });
        console.log("XXXXXXXXX");
    })
);

I followed an example that you can find here. But when I run the code, I have "Error: Cannot enqueue Query after invoking quit" approximatively at line "connection.query("SELECT * FROM user WHERE email = '" + email+"'", function(err, rows) {...". I tried many tricks found on the web but nothing works.

Maybe my question is stupid. But I'm new so thank you for your answers :)



via phenric

No comments:

Post a Comment