gpt4 book ai didi

node.js - Nodejs 和 PassportJs : Redirect middleware after passport. 如果身份验证失败,则不会调用身份验证

转载 作者:IT老高 更新时间:2023-10-28 23:06:54 24 4
gpt4 key购买 nike

我没有登录页面,而是在每个页面上都有一个登录表单。无论身份验证是否成功(使用适当的闪存消息),我都想将用户重定向回他们所在的同一页面

取以下代码:

app.post('/login', validateLogin, passport.authenticate('local-login'), function(req, res) {

var redirectUrl = '/';

if(req.body.to.length > 0){
redirectUrl = req.body.to;
}

console.log("THIS IS ONLY CALLED IF passport.authenticate() IS SUCCESSFUL");
res.redirect(redirectUrl);
});

如果身份验证通过,我只会看到上面的最终中间件被调用。如果失败,则 Passport 似乎以获取请求的形式将我重定向到/login。在我的应用中,此页面不存在。

如果我在 Passport 身份验证函数中传递一个额外的选项对象作为参数,那么它可以工作:

app.post('/login', validateLogin, passport.authenticate('local-login', {

successRedirect : '/', // redirect to the secure profile section
failureRedirect : '/signup', // redirect back to the signup page. THIS IS JUST FOR TESTING TO SEE IF THE REDIRECT ON FAIL WORKS.
failureFlash : true, // allow flash messages

}


));

但是这样做我失去了选择将用户重定向到哪里的能力。如果身份验证失败, Passport 似乎可以控制用户被重定向到的位置。我怎样才能解决这个问题?或者它是一个错误?如果身份验证失败, Passport 身份验证必须是链中的最后一个中间件吗?

这是我的本地策略函数调用:

//LOCAL LOGIN

passport.use('local-login', new LocalStrategy({
// by default, local strategy uses username and password, we will override with email
usernameField : 'email',
passwordField : 'password',
passReqToCallback : true // allows us to pass back the entire request to the callback
},
function(req, email, password, done) { // callback with email and password from our form


console.log("IN PASSPORT");

if(email.length == 0 || password.length == 0){

console.log("FIELDS ARE EMPTY");
return done(null, false, req.flash('loginMessage', 'Fill in all values.'));

}

// find a user whose email is the same as the forms email
// we are checking to see if the user trying to login already exists
User.findOne({ 'local.email' : email }, function(err, user) {
// if there are any errors, return the error before anything else



if (err){
return done(err);
console.log("db err");
}
// if no user is found, return the message
if (!user){
console.log("not user");
return done(null, false, req.flash('loginMessage', 'Incorrect details.')); // req.flash is the way to set flashdata using connect-flash
}
// if the user is found but the password is wrong

if (!user.validPassword(password)){
console.log("invalid pw");
return done(null, false, req.flash('loginMessage', 'Incorrect details.')); // create the loginMessage and save it to session as flashdata
}
// all is well, return successful user
console.log("All OK");
return done(null, user);
});

}));

最佳答案

您可以使用上一段中所述的自定义身份验证回调 http://passportjs.org/guide/authenticate/ .

app.post('/login', function(req, res, next) {
passport.authenticate('local', function(err, user, info) {
if (err) { return next(err); }
// Redirect if it fails
if (!user) { return res.redirect('/login'); }
req.logIn(user, function(err) {
if (err) { return next(err); }
// Redirect if it succeeds
return res.redirect('/users/' + user.username);
});
})(req, res, next);
});

关于node.js - Nodejs 和 PassportJs : Redirect middleware after passport. 如果身份验证失败,则不会调用身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22858699/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com