Node.js Passport无法对用户进行deserilize

时间:2017-09-16 10:29:13

标签: javascript node.js passport.js

我的node.js应用程序有两个护照本地策略。一个用于后台用户,另一个用于客户。两种策略都会查询两个不同的表来验证其各自的用户。

以下是passport.js代码

var isOfficeUser = false;

// used to serialize the user for the session
passport.serializeUser(function(user, done) {
    if(isOfficeUser == true){
        done(null, user.idSystemUser);
    } else {
        done(null, user.idGuests);
    }
});

// used to deserialize the user
passport.deserializeUser(function(id, done) {
    var userQuery;

    if(isOfficeUser == true) {
        userQuery = 'SELECT idSystemUser, Name, Email, Phone, (select IsGuest from SystemProfiles where idSystemProfile = ProfileId) as GuestFlag FROM SystemUsers WHERE idSystemUser = ? ';
    } else {
        userQuery = 'SELECT idGuests, Name, Email, RoomId, GuestsCount, Phone, RoomNo, ' +
        '(select DATE_FORMAT(CheckInDT, \'%b %d %Y %h:%i %p\')) as CheckInDTString, ' +
        '(select DATE_FORMAT(CheckOutDT, \'%b %d %Y %h:%i %p\')) as CheckOutDTString, ' +
        '(select IsGuest from SystemProfiles where idSystemProfile = ProfileId) as GuestFlag ' +
        'FROM Guests WHERE idGuests = ? ';
    }

    connectionPool.getConnection(function(err, connection){
        if (err) {
            winston.log('debug', '------------------------------------ Error: ' + err);
            return;
        }
        connection.query(userQuery,[id],function(err, rows){
            if(err) {
                winston.info('info', '----------------------- ERROR: ' + err);
            }
            done(err, rows[0]);
            connection.release();
        });
    });
});


//this stretegy is for guests
passport.use(
    'guest',
    new LocalStrategy({
        // by default, local strategy uses username and password, we will override with email
        usernameField : 'room',
        passwordField : 'accesskey',
        passReqToCallback : true // allows us to pass back the entire request to the callback
    },
    function(req, room, accesskey, done) {
        isOfficeUser = false;
        connectionPool.getConnection(function(err, connection){
            if(err) {
                winston.log('debug', '------------------------------------ Error: ' + err);
                return;
            }
            connection.query('SELECT idGuests, Name, Email, RoomId, RoomNo, GuestsCount, Phone FROM Guests WHERE RoomNo = ? and AccessKey = ?',[room, accesskey], function(err, rows){
                connection.release();
                if(err) {
                    return done(err);
                }

                if(!rows.length) {
                    return done(null, false, {message : {active : true, text : 'Incorrect Credentials'}});
                }

                // all is well, return successful user
                return done(null, rows[0]);
            });
        });
    })
);

//this stretegy is for office user
passport.use(
    'office',
    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) {
        isOfficeUser = true;
        connectionPool.getConnection(function(err, connection){
            if(err) {
                winston.log('debug', '------------------------------------ Error: ' + err);
                return;
            }
            connection.query('SELECT idSystemUser, Name, Active, Email, ProfileId, Phone, Password FROM SystemUsers WHERE Email = ?',[email], function(err, rows){
                connection.release();
                if(err) {
                    return done(err);
                }

                if(!rows.length) {
                    return done(null, false, {message : {active : true, text : 'Incorrect Credentials'}});
                }

                //if the user access is revoked
                if(rows[0].Active == 0) {
                    return done(null, false, {message : {active : true, text : 'Your administrator has revoked your access to portal. Please contact your Administrator to get this resolved.'}});
                }

                // if the user is found but the password is wrong
                if(!bcrypt.compareSync(password, rows[0].Password)) {
                    return done(null, false, {message : {active : true, text : 'Incorrect Email or Password'}});
                }
                // all is well, return successful user
                return done(null, rows[0]);
            });
        });
    })
);

但是,出于某种原因,我退出了会话并显示错误:

  

"未能将用户反序列化为会话"

我必须刷新浏览器并重新登录,但过了一段时间后它会再次关闭我。

我在这里做错了什么?

0 个答案:

没有答案