【问题标题】:My authentication system is returning undefined我的身份验证系统返回未定义
【发布时间】:2013-06-15 14:46:47
【问题描述】:

我有一个使用护照的身份验证系统,我不知道为什么,但它返回“未定义” 值,有人可以帮助我,下面是我的代码:

app.js

passport.use(new LocalStrategy(function (username, password, done) {
  Person.findOne({ 'account.username' : username }, function (err, person) {

   if (err) {
     return done(err);
   }

   if (!profile) {
     return done(null, false, { message: 'Invalid username or password' });
   }

   // My self defined function that encrypts the password
   var encryptedPassword = myFunc.encrypt(password);

   if (person.account.password !== encryptedPassword) {
     return done(null, false, { message: 'Invalid username or password' });
   } else {
     return done(null, profile);
   }

  });
}));

person.js

//POST: /login
app.post('/login', passport.authenticate('local', {failureRedirect: '/login',
  failureFlash: true}), 
    function (req, res) {
      console.log(req.profile);
      res.redirect('/home');
    }
  );

//GET: /home
app.get('/home', function (req, res) {
  console.log(req.profile);  // Returns Undefined
});

当我渲染具有 req.profile 变量的页面时也会发生这种情况。

【问题讨论】:

  • 第一条路由 (POST /login) 是否真的为req.profile 记录了一些内容?如果登录会话处于活动状态,Passport 将填充 req.user,但不会填充 req.profile
  • 谢谢,我搞定了,非常感谢

标签: javascript node.js express mongoose passport.js


【解决方案1】:

使用 req.user 而不是 req.profile

【讨论】:

    猜你喜欢
    • 2018-10-15
    • 1970-01-01
    • 2015-07-01
    • 1970-01-01
    • 2020-06-17
    • 1970-01-01
    • 1970-01-01
    • 2020-12-17
    • 1970-01-01
    相关资源
    最近更新 更多