Hapi js身份验证架构实现错误/错误?

时间:2017-11-28 03:47:14

标签: node.js hapijs

我有像这样的身份验证模式实现

const schemeImplementation = (server, options) => {
  return {
    authenticate: (request, reply) => {
      authenticate(request)
      .then((credentials) => {
        return reply.continue({ credentials });
      })
      .catch((err) => {
        // not returning any response instead of timeout
        return reply(err);
      });
    }
  };
};

和路线处理程序

handler: (req, reply) => {
   throw new Error('This shouldn\'t lead to a timeout');
}

我希望它能正确返回错误响应。 Hapi之前有没有人经历过这个?

  

注意:只有在同步抛出异常时才会发生   参考:https://hapijs.com/api#authentication-scheme

1 个答案:

答案 0 :(得分:0)

IMO,通过Boom插件重新发送失败的更好方法。我这样使用: -

const schemeImplementation = (server, options) => {
  return {
    authenticate: (request, reply) => {
      authenticate(request)
      .then((credentials) => {
        return reply.continue({ credentials });
      })
      .catch((err) => {
        // this way it will directly return the error to client and would not go handler
        reply(Boom.badGateway(err));
      });
    }
  };
};