Piet
Piet

Reputation: 415

passport-local with mysql not working

I am using node.js and passport and mysql for a userlogin. I the main source is from https://github.com/manjeshpv/node-express-passport-mysql/issues

I want to add more columns in the table. I started with emailfield and change the code like below. I simply added the email variable at the needed places I think. I cannot find the bug where its crashing. without modifying anything, the code does work.

passport.js:

passport.use(
    'local-signup',
    new LocalStrategy({
        // by default, local strategy uses username and password, we will override with email
        usernameField : 'username',
        passwordField : 'password',
        //emailField : 'email',
        passReqToCallback : true // allows us to pass back the entire request to the callback
    },
    function(req, username, password, email, 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 users WHERE username = ?",[username], function(err, rows) {
            if (err)
                log.info(err);
                //return done(err);
            if (rows.length) {
                return done(null, false, req.flash('signupMessage', 'That username is already taken.'));
            } else {
                // if there is no user with that username
                // create the user
                var newUserMysql = {
                    username: username,
                    email: email,
                    password: bcrypt.hashSync(password, null, null)  // use the generateHash function in our user model

                };

                var insertQuery = "INSERT INTO users ( username, password, email ) values (?,?,?)";
                connection.query(insertQuery,[newUserMysql.username, newUserMysql.password, newUserMysql.email],function(err, rows) {
                    newUserMysql.id = rows.insertId;

                    return done(null, newUserMysql);
                });
            }
        });
    })
);

and here the log:

The magic happens on port 8080
GET /signup 200 20ms - 1.21kb
D:\node-express-passport-mysql\node_modules\mysql\lib\protocol\Parser.js:82
        throw err;
              ^
TypeError: undefined is not a function
    at Object.SqlString.escape (D:\node-express-passport-mysql\node_modules\mysq
l\lib\protocol\SqlString.js:46:13)
    at D:\node-express-passport-mysql\node_modules\mysql\lib\protocol\SqlString.
js:80:19
    at String.replace (native)
    at Object.SqlString.format (D:\node-express-passport-mysql\node_modules\mysq
l\lib\protocol\SqlString.js:71:14)
    at Connection.format (D:\node-express-passport-mysql\node_modules\mysql\lib\
Connection.js:263:20)
    at Connection.query (D:\node-express-passport-mysql\node_modules\mysql\lib\C
onnection.js:196:22)
    at Query._callback (D:\node-express-passport-mysql\config\passport.js:71:32)

    at Query.Sequence.end (D:\node-express-passport-mysql\node_modules\mysql\lib
\protocol\sequences\Sequence.js:96:24)
    at Query._handleFinalResultPacket (D:\node-express-passport-mysql\node_modul
es\mysql\lib\protocol\sequences\Query.js:144:8)
    at Query.EofPacket (D:\node-express-passport-mysql\node_modules\mysql\lib\pr
otocol\sequences\Query.js:128:8)
28 Jun 21:03:58 - [nodemon] app crashed - waiting for file changes before starti
ng...

Upvotes: 0

Views: 770

Answers (1)

robertklep
robertklep

Reputation: 203304

This looks to be the problem:

function(req, username, password, email, done) {

You added an extra argument email which shouldn't be there. Since it clobbers the done callback, when your code tries to call it it will cause an "undefined is not a function" error.

If you're passing an extra email property, you can access it through req.body.email (assuming that you're using a POST route to log in).

Upvotes: 1

Related Questions