我在我的node / express应用程序中使用Passport.js进行身份验证,当输入字段为空时,护照显示默认消息"缺少凭据",我该如何更改?说一些像请填写表格或其他东西
这是我的本地登录策略:
passport.use('login', new LocalStrategy({
usernameField: 'email',
passwordField: 'password'
},
function(email, password, done) {
models.User.findOne({where: {email}}).then(user => {
if (!user)
return done(null, false, {message: 'Email not registered'});
user.checkPassword(password).then(res => {
if (!res)
return done(null, false, {message: 'Incorrect username or password'});
return done(null, user);
});
});
}));
登录路线:
router.post('/login', passport.authenticate('login', {
successRedirect: '/',
failureRedirect: '/login',
failureFlash: true
}));
答案 0 :(得分:2)
我明白了,看起来我可以通过可选的回调来验证功能以覆盖默认功能,感谢oneturkmen以供参考。
这里修改了路线:
router.post('/signup', (req, res) => {
passport.authenticate('signup', (err, user, info) => {
const body = req.body;
if (req.body.username === '' || req.body.email === '' || req.body.password === '')
info.message = 'Please fill the form';
req.logIn(user, err => {
if (err) res.render('users/signup', info);
else res.redirect('/');
});
})(req, res);
});
答案 1 :(得分:0)
另一种方法是在代码中添加“ badRequestMessage ”
您的代码将如下所示
router.post('/login', passport.authenticate('login', {
successRedirect: '/',
failureRedirect: '/login',
badRequestMessage : 'Please fill the form',
failureFlash: true
}));