Passport.authenticate() 路由内处理错误?
Passport.authenticate() Error handling inside a route?
如果我想处理来自此路由上“不正确”登录的错误,如何收集(错误)以及在哪里收集?
// POST LOGIN
usersRouter
.route('/login')
.post(passport.authenticate('local'), (req, res, next) => {
// for example, to declare an if here to catch err and call next with err...
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.json({ success: true, status: 'You are successfully logged in!' });
});
你可以这样使用
usersRouter.post('/login', function(req, res, next) {
passport.authenticate('local', function(err, user, info) {
if (err) { return next(err); }
if (!user) {
return res.send(401,{ success : false, message : 'authentication failed' });
}
return res.send({ success : true, message : 'authentication succeeded' });
})(req, res, next);
});
如果我想处理来自此路由上“不正确”登录的错误,如何收集(错误)以及在哪里收集?
// POST LOGIN
usersRouter
.route('/login')
.post(passport.authenticate('local'), (req, res, next) => {
// for example, to declare an if here to catch err and call next with err...
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.json({ success: true, status: 'You are successfully logged in!' });
});
你可以这样使用
usersRouter.post('/login', function(req, res, next) {
passport.authenticate('local', function(err, user, info) {
if (err) { return next(err); }
if (!user) {
return res.send(401,{ success : false, message : 'authentication failed' });
}
return res.send({ success : true, message : 'authentication succeeded' });
})(req, res, next);
});