0
votes

Creating a user registration functionality using Passport(Local-Signup), code below:

// config/passport.js

// load all the things we need
var LocalStrategy   = require('passport-local').Strategy;

// load up the user model
var mysql = require('mysql');
var bcrypt = require('bcrypt-nodejs');
var dbconfig = require('./database');
var connection = mysql.createConnection(dbconfig.connection);

connection.query('USE ' + dbconfig.database);
// expose this function to our app using module.exports
module.exports = function(passport) {


    passport.serializeUser(function(user, done) {
        done(null, user.id);
    });

    // used to deserialize the user
    passport.deserializeUser(function(id, done) {
        connection.query("SELECT * FROM users WHERE id = ? ",[id], function(err, rows){
            done(err, rows[0]);
        });
    });


    passport.use(
        'local-signup',
        new LocalStrategy({
            // by default, local strategy uses username and password, we will override with email
            usernameField : 'username',
            passwordField : 'password',
            passReqToCallback : true // allows us to pass back the entire request to the callback
        },
        function(req, username, 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 users WHERE username = ?",[username], function(err, rows) {
                if (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,
                        password: bcrypt.hashSync(password, null, null)  // use the generateHash function in our user model
                    };

                    var insertQuery = "INSERT INTO users ( username, password ) values (?,?)";

                    connection.query(insertQuery,[newUserMysql.username, newUserMysql.password],function(err, rows) {
                        newUserMysql.id = rows.insertId;

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

throws the following error:

TypeError: Cannot read property 'id' of undefined at Query._callback (M:\server\config\passport.js:70:55) at Query.Sequence.end (M:\server\node_modules\mysql\lib\protocol\sequences\Sequence.js:88:24) at Query.ErrorPacket (M:\server\node_modules\mysql\lib\protocol\sequences\Query.js:90:8) at Protocol._parsePacket (M:\server\node_modules\mysql\lib\protocol\Protocol.js:279:23) at Parser.write (M:\server\node_modules\mysql\lib\protocol\Parser.js:76:12) at Protocol.write (M:\server\node_modules\mysql\lib\protocol\Protocol.js:39:16) at Socket. (M:\server\node_modules\mysql\lib\Connection.js:103:28) at emitOne (events.js:116:13) at Socket.emit (events.js:211:7) at addChunk (_stream_readable.js:263:12) at readableAddChunk (_stream_readable.js:250:11) at Socket.Readable.push (_stream_readable.js:208:10) at TCP.onread (net.js:594:20)

Using console.log(rows); returns undefined. How can I address this issue?

[EDIT] After answer was taken into account the following error was produced (because rows.insertId is still undefined):

Error: Failed to serialize user into session at pass (M:\server\node_modules\passport\lib\authenticator.js:281:19) at serialized (M:\server\node_modules\passport\lib\authenticator.js:286:7) at M:\server\config\passport.js:24:9 at pass (M:\server\node_modules\passport\lib\authenticator.js:294:9) at Authenticator.serializeUser (M:\server\node_modules\passport\lib\authenticator.js:299:5) at SessionManager.logIn (M:\server\node_modules\passport\lib\sessionmanager.js:14:8) at IncomingMessage.req.login.req.logIn (M:\server\node_modules\passport\lib\http\request.js:50:33) at Strategy.strategy.success (M:\server\node_modules\passport\lib\middleware\authenticate.js:248:13) at verified (M:\server\node_modules\passport-local\lib\strategy.js:83:10)

2
Does err contain something?Hammerbot
@El_Matella nope error is null. Re-running the code produced another error now: Error: Failed to serialize user into sessionValamorde
where is newUserMysql coming from ?Ravi
@Ravi it's just a local var in JSON format. var newUserMysql = {username: username, password : password};Valamorde
@Valamorde can you share the json as well for reference ?Ravi

2 Answers

2
votes

As per my understanding, it is nothing to do with MySQL

The JSON, which you shared doesn't have id

var newUserMysql = {username: username, password : password};

whereas, you are trying to access id and that's the reason, you getting undefined

newUserMysql.id = rows.insertId;

To solve this issue, you need to add id to your JSON.

var newUserMysql = {username: "username", password : "password", id:""};
0
votes

The answer was under my nose under the whole time. All I had to do was to change this:

newUserMysql = rows.insertId;

into this:

newUserMysql = rows[0].insertId;

and the issue was resolved successfully.