快递js设置护照本地策略?

时间:2018-06-11 13:34:13

标签: express passport.js passport-local

我搜索了谷歌关于如何设置护照会话。这是我的代码,在我的index.js文件中,我将护照js配置为

app.use(bodyParser.json({limit: '50mb'}));    
app.use(passport.initialize());
app.use(passport.session());

passport.use('local-login', new LocalStrategy(function(userName, password, userCategory, done) {
  console.log('Local login');
  User.findOne({'userName':userName,'userCategory':userCategory}, function(err, user) {
    if (err){
      return done(err);
    }else{
      if (!user){
        return done(null, false, 'Incorrect username.' );
      }else{
        user.verifyPassword(password, function(err, isMatch) {
          if (isMatch) {
            return done(null, user);
          } else {
            return done(null, false, 'Incorrect password.');
          }
        });
      }
    }
  });
}));

//Passport also needs to serialize and deserialize user instance from a session store in order to support login sessions, 
//so that every subsequent request will not contain the user credentials.
passport.serializeUser(function(user, done) {
  console.log('Passport:'+user);
  var sessionUser = { _id: user._id, userName: user.userName, userCategory: user.userCategory };
  done(null, sessionUser);
});

passport.deserializeUser(function(id, done) {
  console.log('Passport:'+id);
  User.findById(id, function(err, user) {
    done(err, user);
  });
});

passport.use(new LocalStrategy(User.authenticate()));

var router = express.Router();
//require('./controllers/vendor')(router);
require('./controllers/user')(router,passport);

然后在我的模型中,我将其插件添加为UserAccountSchema.plugin(passportLocalMongoose);

然后在我的控制器文件中使用

passport.authenticate('local-login', function(err, user, info) {
            if (err || !user) {
                console.log('Error:'+JSON.stringify(err));
                response.status(400).send(err);
            } else {
                console.log('passport:'+user);
            }
        })(request, response, next);

当我尝试登录时,它没有调用本地策略中间件。没有打印控制台。有人可以帮我解决这个问题吗?谢谢

0 个答案:

没有答案