Passport-jwt令牌从服务器获取错误消息

时间:2017-07-25 13:34:07

标签: express passport.js passport-local

我根本无法弄清楚如何将错误消息从服务器传递到客户端。我搜索了文档,但无法在哪里实现回调函数以返回错误消息。

我的服务器端代码如下所示:

passport.js

var localLogin = new LocalStrategy(localOptions, function(email, password, done){

User.findOne({
  email: email
 }, function(err, user, info){

if(err){
  return done(err);
}

if(!user){
  return done(null, false, {message: 'User not found'});
}

user.comparePassword(password, function(err, isMatch){

  if(err){
    return done(err);
  }

  if(!isMatch){
    return done(null, false, {message: 'Passwords does not match'});
  }

  return done(null, user);

});

});

});

router.js

> var requireAuth = passport.authenticate('jwt', {session: false}),
>     requireLogin = passport.authenticate('local', {session: false});
> 
> module.exports = function(app){
> 
>   var apiRoutes = express.Router(),
>       authRoutes = express.Router(),
>       todoRoutes = express.Router();
> 
>   // Auth Routes   apiRoutes.use('/auth', authRoutes);
> 
>   authRoutes.post('/register', AuthenticationController.register);  
> authRoutes.post('/login', requireLogin, AuthenticationController.login);

在客户端中,我收到此错误消息。

Response {_body: "Unauthorized", status: 401, ok: false, statusText: "Unauthorized", headers: Headers…}headers: Headersok: falsestatus: 401statusText: "Unauthorized"type: 2url: "http://localhost:8083/api/auth/login"_body: "Unauthorized"__proto__: Body

0 个答案:

没有答案