使用Express,Passport js本地策略中的重定向失败

时间:2017-07-18 20:17:43

标签: node.js express passport.js

我是护照js的新手。我的登录重定向总是由于某种原因失败,最终以failureRedirect.Below为代码片段。 当我单击Login时,作为响应,它会将我重定向回Login页面,因为failureRedirect设置为/ login。没有其他错误消息。 有什么方法可以调试护照js或任何其他建议来解决这个问题吗?

$('#signin-button').click(function () {

var username = $('#signin-email').val();
var password = $('#signin-password').val();
var form = new FormData();
form.append("username", username);
form.append("password", password);
//alert ('email/pass:' + username + ", " + password);
var settings = {
  "async": true,
  "crossDomain": true,
  "url": "/login",
  "method": "POST",
  "headers": {
    "cache-control": "no-cache",
    "postman-token": "ab1b08a7-1a77-97e9-1039-92479d10b68b"
  },
  "processData": false,
  "contentType": false,
  "mimeType": "multipart/form-data",
  "data": {"username": username,"password": password}
}

$.ajax(settings).done(function (response) {
  console.log(response);
});
});

路线:

app.post('/login',passport.authenticate('local-login',
{            successRedirect : '/dashboard', 
            failureRedirect : '/login'
}));

Passport.js

 passport.use(
        'local-login',
        new LocalStrategy({
    usernameField: 'username',
    passwordField: 'password',
    passReqToCallback: true
},
        function(req, username, password, done) {               
            connection.query("SELECT * FROM user WHERE user_name = ?",[username], function(err, rows){
                if (err)
                    return done(err);
                if (!rows.length) {
                    return done(null, false});
                }

                // if the user is found but the password is wrong
                if (!bcrypt.compareSync(password, rows[0].password))
                    return done(null, false);     
                // all is well, return successful user
                return done(null, rows[0]);
            });
        })
    );

0 个答案:

没有答案