Node.js Passport身份验证-未定义日志错误

时间:2018-08-03 00:06:30

标签: javascript node.js passport.js

我正在使用node.js,passport和postgres在用户登录页面上工作。我以为我可以进行用户身份验证了。但是,当我尝试更改页面时,出现此错误,并且服务器不会加载任何内容。 {"message":"log is not defined","error":{}}。我已经在这个错误上停留了几个小时,无法弄清楚是什么原因造成的。我猜该会议不知何故变得混乱了,但我可能是错的。我希望有人知道答案。这是我的相关代码。

我对Web开发非常陌生,因此,我确信这可能是我所缺少的简单事物。

main-config.js

(function(appConfig) {

'use strict';

// *** main dependencies *** //
const path = require('path');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const session = require('express-session');
const flash = require('connect-flash');
const morgan = require('morgan');
const nunjucks = require('nunjucks');
const passport = require('passport');

// *** view folders *** //
const viewFolders = [
path.join(__dirname, '..', 'views')
];

// *** load environment variables *** //
require('dotenv').config();

appConfig.init = function(app, express) {

// *** view engine *** //
nunjucks.configure(viewFolders, {
  express: app,
  autoescape: true
});
app.set('view engine', 'html');

// *** app middleware *** //
if (process.env.NODE_ENV !== 'test') {
  app.use(morgan('dev'));
}
app.use(cookieParser());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(session({
  secret: 'anything',
  resave: false,
  saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
app.use(express.static(path.join(__dirname, '..', '..', 'client')));

  };

})(module.exports);

local.js

passport.use(new LocalStrategy({
  usernameField: 'email',
  passwordField: 'password'
  },
  (username, password, done) => {
  return db.one("SELECT * " +
    "FROM Users " +
    "WHERE Email=$1", [username])
  .then((result)=> {
    return done(null, result);
  })
  .catch((err) => {
    return done(null, false, {message:'Wrong user name or password'});
  });
}));

passport.js

module.exports = () => {

  passport.serializeUser((user, done) => {
    done(null, user.userid);
  });

  passport.deserializeUser((id, done)=>{
    log.debug("deserialize ", id);
    db.one("SELECT * FROM User " +
            "WHERE user_id = $1", id)
    .then((user)=>{
      done(null, user);
    })
    .catch((err)=>{
      done(new Error(`User with the id ${id} does not exist`));
    })
  });
};

auth.js

router.post('/login', authHelpers.loginRedirect, (req, res, next) => {
  passport.authenticate('local', (err, user, info) => {
    if (err) { handleResponse(res, 500, 'error'); }
    if (!user) {
      console.log("User Not Found");
      handleResponse(res, 404, 'User not found');
    }
    if (user) {
      req.logIn(user, function (err) {
        if (err) { handleResponse(res, 500, 'error'); }
        handleResponse(res, 200, 'success');
      });
    }
  })(req, res, next);
});

1 个答案:

答案 0 :(得分:1)

这是明确的错误消息-log未在护照的以下行中定义

log.debug("deserialize ", id);

使用记录器定义日志对象,一切都会很好。或者暂时将其删除。