Omi*_*rai 65 node.js express passport.js
我已经看过错误处理应该如何通过这个堆栈交换在节点中工作,但我不确定护照在认证失败时正在做什么.我有以下LocalStrategy:
passport.use(new LocalStrategy({ usernameField: 'email', passwordField: 'password' },
function(email, password, next) {
User.find({email: UemOrUnm}, function(err, user){
if (err) { console.log('Error > some err'); return next(err); }
if (!user) { console.log('Error > no user'); return next('Incorrect login or password'); }
if (password != user.password) {
return next(Incorrect login or password);
}
return next(null, user);
});
}
));
Run Code Online (Sandbox Code Playgroud)
在我看到'错误>一些错误的'控制台打印输出后,没有其他事情发生.我认为它应该继续在下一个带有错误参数的路径上,但它似乎没有这样做.这是怎么回事?
rob*_*lep 157
策略实现与passport.authenticate验证请求和处理成功/失败一起使用.
假设您正在使用此路由(传递电子邮件地址和密码):
app.post('/login', passport.authenticate('local', {
successRedirect: '/loggedin',
failureRedirect: '/login', // see text
failureFlash: true // optional, see text as well
});
Run Code Online (Sandbox Code Playgroud)
这将调用策略中的代码,其中可能发生以下三种情况之一:
next(err); 这将由Express处理并生成HTTP 500响应;false作为用户对象传递:next(null, false); 这将触发failureRedirect(如果你没有定义一个,将生成HTTP 401 Unauthorized响应);next(null, user); 这将触发successRedirect;如果身份验证无效(但不是内部错误),您可以传递额外的消息以及回调:
next(null, false, { message : 'invalid e-mail address or password' });
Run Code Online (Sandbox Code Playgroud)
如果您已经使用failureFlash 并安装了connect-flash中间件,则提供的消息将存储在会话中,并且可以轻松访问,例如,在模板中使用.
编辑:它也可以自己完全处理身份验证过程的结果(而不是Passport发送重定向或401):
app.post('/login', function(req, res, next) {
passport.authenticate('local', function(err, user, info) {
if (err) {
return next(err); // will generate a 500 error
}
// Generate a JSON response reflecting authentication status
if (! user) {
return res.send({ success : false, message : 'authentication failed' });
}
// ***********************************************************************
// "Note that when using a custom callback, it becomes the application's
// responsibility to establish a session (by calling req.login()) and send
// a response."
// Source: http://passportjs.org/docs
// ***********************************************************************
req.login(user, loginErr => {
if (loginErr) {
return next(loginErr);
}
return res.send({ success : true, message : 'authentication succeeded' });
});
})(req, res, next);
});
Run Code Online (Sandbox Code Playgroud)
小智 18
基督徒所说的是你需要添加这个功能
req.login(user, function(err){
if(err){
return next(err);
}
return res.send({success:true});
});
Run Code Online (Sandbox Code Playgroud)
所以整个路线将是:
app.post('/login', function(req, res, next) {
passport.authenticate('local', function(err, user, info) {
if (err) {
return next(err); // will generate a 500 error
}
// Generate a JSON response reflecting authentication status
if (! user) {
return res.send(401,{ success : false, message : 'authentication failed' });
}
req.login(user, function(err){
if(err){
return next(err);
}
return res.send({ success : true, message : 'authentication succeeded' });
});
})(req, res, next);
});
Run Code Online (Sandbox Code Playgroud)
来源:http://passportjs.org/guide/login/
| 归档时间: |
|
| 查看次数: |
62528 次 |
| 最近记录: |