Passport.js成功的身份验证根本没有调用
Passport.js successful authentication not calling at all
这是我的代码:
passport.use(new localStrategy(
function(username, password, done) {
User.findOne({
username: username
}, function(err, user) {
if (err) throw err;
if (!user) {
console.log("Unknown User");
return done(null, false, {
message: "Unknown User"
})
}
if (!user.validPassword(password)) {
console.log("Incorrect password.", password);
return done(null, false, {
message: 'Incorrect password.'
});
}
console.log("User Is detected");
return done(null, user, {
message: "User Is detected"
})
});
}
));
router.post('/login',
passport.authenticate('local', {
successRedirect: '/',
failureRedirect: '/users/login',
failureFlash: true,
successFlash: 'Welcome!'
}),
function(req, res) { // this function not called
console.log(req)
});
一切都很好,但成功函数没有调用,我想在登录后收到这样的消息:"welcome"+req.body.username
。
有什么建议吗?thx
来自passport.authenticate()
:的文档
重定向选项覆盖默认行为
在您的示例中,因为您设置了successRedirect
选项,所以在成功进行身份验证时不会调用中间件链中的下一个函数。
如果你想在重定向到'/'
之前调用你的函数,那么你应该:
router.post('/login',
passport.authenticate('local', {
failureRedirect: '/users/login',
failureFlash: true
}),
function(req, res) {
// This should show up in your logs:
console.log('Welcome ' + req.body.username);
// You can also use a flash to consume after redirect:
// (provided that you use connect-flash in your app)
req.flash('info', 'Welcome ' + req.body.username);
res.redirect('/');
}
);
相关文章:
- Passport.js成功的身份验证根本没有调用
- 使用 feathersjs 通过简单的 rest 调用进行身份验证
- 如何使用 Javascript 对 SharePoint REST 调用进行身份验证
- 保留 Azure 移动服务的自定义 API 调用的用户身份验证
- 不调用的 Passportjs 是身份验证
- Google Plus api 调用未经身份验证
- 如何使用基本身份验证从 angularjs 调用 rest 服务
- Azure Table Storage 通过 javascript 对 REST 调用进行身份验证
- 护照.js身份验证成功,未调用 next()
- 如何使用 JavaScript 调用 HTTPS 身份验证
- 使用HTTP基本身份验证的JQuery Ajax调用
- 对passportjs+Node.js+Express.js Rest API调用成功身份验证和失败的路由处理程序
- 身份验证-$http'调用了s.then()成功回调,而不是错误回调
- 推送器:“;未捕获的类型错误:无法调用方法'scopedTo'未定义的“;进行身份验证时
- 在MVC控制器和JavaScript Ajax调用之间需要身份验证
- 使用activedirectory身份验证在JavaScript中调用Azure API应用程序
- 在调用REST端点之前进行身份验证
- 对服务帐户进行身份验证,以便使用JavaScript客户端库调用Google API
- 为使用Azure Active Directory身份验证保护的Web API的AJAX调用添加身份验证令牌
- 使用Angular服务的请求拦截器对REST get调用进行简单的身份验证