简体   繁体   English

Passport.js验证始终失败

[英]Passport.js verification always fails

My passport middleware is not working. 我的护照中间件不起作用。 When I call passport.authenticate() on a route to confirm the user is authenticated, I am redirected to the failure page. 当我在路由上调用passport.authenticate()确认用户已通过身份验证时,我将重定向到故障页面。 The login route however works as expected, and I am redirected successfully to the correct page, where the failure middleware redirect is called and I am sent back to the login page. 但是,登录路由按预期方式工作,并且我成功重定向到正确的页面,在该页面中调用了失败的中间件重定向,并将我发送回登录页面。

I have a Passport strategy like so: 我有这样的护照策略:

module.exports = function(){ 
  var passport = require('passport');
  var LocalStrategy = require('passport-local').Strategy
  var User = require('../models/user');
  // used to serialize the user for the session
  passport.serializeUser(function(user, done) {
    done(null, user.id);
  });

  // used to deserialize the user
  passport.deserializeUser(function(id, done) {
    User.getUserById(id, function(err, user) {
      done(err, user);
    });
  });

  passport.use(new LocalStrategy({
    usernameField: "email",
    passwordField: "password"
  }, function(email, password, done){
    User.getUserByEmail(email, function(err, user){
      if(err) throw err;
      if(!user){
        console.log('unknown user');
        return done(null, false, {message: 'Email not recognised, please try again'});  
      } 
      User.comparePassword(password, user.password, function(err, isMatch){
        if(err) throw err;
        if(isMatch){
          return done(null, user);
        } else {
          console.log('Invalid password');
          return done(null, false, { message: 'Password not recognised, please try again' });
        }
      });
    });
  }));
}; 

Logging in is fine using: 可以使用以下方法登录:

router.post('/user/login', 
  passport.authenticate('local', { 
    successRedirect: '/clients',
    failureRedirect: '/user/login', 
    failureFlash: true
  }
));

My '/clients' route is like so, where the authentication fails and redirects incorrectly back to the login page: 我的'/ clients'路由是这样的,其中身份验证失败并且错误地重定向回登录页面:

router.get('/clients', 
  passport.authenticate("local", { 
    failureRedirect: "/user/login",
    failureFlash: "not verified" 
  }), function(req, res, next) {
  res.locals.client = null;
  Client.find({})
    .select("name")
    .select("_id") 
    .exec(function(err, clients){
      res.render('client/views/clients', { 
        title: 'Clients',
        clients: clients 
      });
  });  
});

Server passport initialization like so: 服务器护照初始化如下:

//passport
app.use(passport.initialize());
app.use(passport.session());
app.use(bodyParser.urlencoded({ extended: false }));
require("./user/services/passport")(passport);

What am I doing wrong? 我究竟做错了什么?

As you have used failureRedirect: '/user/login' . 当您使用了failureRedirect:'/ user / login'。 It will redirect you to the login page. 它将把您重定向到登录页面。 If you want to redirect it to someother page change the failureRedirect value to that corresponding route. 如果要将其重定向到其他页面,请将failureRedirect值更改为相应的路由。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM