未调用 Hapi 基本身份验证验证

Hapi basic auth validate is not called

基本的验证函数

await server.register(require('@hapi/basic'));
const validate = async (request, email, password, id_customer) => {
    console.log(request)
        if (!email || !password || !id_customer) {
            return { credentials: null, isValid: false };
        }
    
        const results = await getHash(id_customer);
    
        if (results.length == 0) {
            return { credentials: null, isValid: false };
        }
    
        if (bcrypt.compareSync(password, results[0]['passwd'])) {
            const credentials = { id: id_customer, email: email };
    
            return { isValid: true, credentials };
        }
        return { credentials: null, isValid: false };
    };
server.auth.strategy('simple', 'basic', { validate });

路线示例:

{
    method: 'POST',
    path: '/home/getCategories',
    config: {
        auth: 'simple',
        description: 'Get Home',
        payload: {
            multipart: true
        },
        handler: Home.getCategories
    },
   /* options: {
        auth: 'simple'
    },*/
    //handler: Home.getCategories
},

这是来自应用程序的 axios 调用:

axios.post('https://api.domain.com/home/getCategories', {
    code: code
  },
  {
    headers: {
        'email': email,
        'password': password,
        'id_customer': id_customer
    },
  })

当我拨打电话时,我收到了未经授权的 401,但我看不到 'console.log(request)'

的输出

有什么帮助吗?

您尝试过以下方法吗?您使用的 Hapi.js 是什么版本?

const categoryPostValidation = {
  payload: Joi.object({
    name: Joi.string().label("Name").min(1).max(30).error((errors) => new Error('Name is invalid, and must be 1 to 30 characters in length')).required(),
    description: Joi.string().label("Description").min(1).max(255).error((errors) => new Error('Description is invalid, and must be 1 to 255 characters in length')).required()
  }),
  failAction: async (request, h, err) => {
    throw err;
  }
};

const categoryPostRouteOptions = {
  description: "Posts one category.",
  cors: true,
  payload: {
    output: 'data', // These are default options
    parse: true // These are default options
  },
  auth: {
      mode: 'required' // or 'try', etc
      strategy: 'simple'
  },
  validate: categoryPostValidation,
  handler: Home.getCategories
};


{
    method: 'POST',
    path: '/home/getCategories',
    options: categoryPostRouteOptions
},