【问题标题】:How can i access user values for my application如何访问我的应用程序的用户值
【发布时间】:2020-06-20 07:33:11
【问题描述】:

当我在'/login' 中打印req.user 时效果很好,但是当我在其他操作('/''/profile 等...)中使用相同的代码时它不起作用

//GET login
router.get('/login', (req, res, next)=>{

  var messageLoginErr = req.flash('signinError')
  res.render('login', {messages : messageLoginErr}); 
});


router.post('/login', passport.authenticate('local', {
  session: false,
  //successRedirect: '/',
  failureRedirect: '/login',
  //failureFlash: true,
}),
  function(req, res, next){
    res.json({email : req.user.email})
  }
);

// // /* GET home page. */
router.get('/', function (req, res, next) {
  res.json({email : req})
});
});

【问题讨论】:

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


    【解决方案1】:

    没有看到您的护照设置,这就是我对问题的假设:

    /* assuming you're using passport-local along with passport, as well as the 
    standard library express-session, and have defined a User model */
    const passport = require('passport');
    const LocalStrategy = require('passport-local').Strategy;
    const expressSession = require('express-session');
    const User = require('./models/user');
    
    
    passport.use(User.createStrategy());
    
    // use static authenticate method of model in LocalStrategy
    passport.use(new LocalStrategy(User.authenticate()));
    
    /* this is where I think the issue might be, that maybe the User hasn't been serialized */
    passport.serializeUser(User.serializeUser());
    passport.deserializeUser(User.deserializeUser());
    

    我是新人,所以无法发表评论要求澄清。如果有人想编辑,请继续。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-26
      • 2020-07-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多