我正在研究hapi js API基本身份验证,并且正在使用有关身份验证的Hapi文档。我相信我所做的一切都正确,但是我收到有关UnhandledPromiseRejectionWarning的错误提示。请帮忙

index.js

'use strict';

const Bcrypt = require('bcrypt');
const Hapi = require('hapi');
const Basic = require('hapi-auth-basic');

const server = new Hapi.Server({
  host: 'localhost',
  port: 3000
})

const users = {
    john: {
        username: 'john',
        password: '$2a$10$iqJSHD.BGr0E2IxQwYgJmeP3NvhPrXAeLSaGCj6IR/XU5QtjVu5Tm',
        name: 'John Doe',
        id: '2133d32a'
    }
};

const validate = function (request, username, password, callback) {
    const user = users[username];
    if (!user) {
        return callback(null, false);
    }

Bcrypt.compare(password, user.password, (err, isValid) => {
    callback(err, isValid, { id: user.id, name: user.name });
});
};

server.register(Basic, (err) => {

if (err) {
    throw err;
}

server.auth.strategy('simple', 'basic', { validateFunc: validate });
server.route({
    method: 'GET',
    path: '/',
    config: {
        auth: 'simple',
        handler: function (request, reply) {
            reply('hello, ' + request.auth.credentials.name);
        }
    }
});

server.start((err) => {

    if (err) {
        throw err;
    }

    console.log('server running at: ' + server.info.uri);
});
});


package.json

    "bcrypt": "^1.0.3",
    "hapi-auth-basic": "^5.0.0",
    "hapi": "^17.1.0"


错误

(node:1248) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): AssertionError [ERR_ASSERTION]: Invalid register options "value" must be an object
(node:1248) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

最佳答案

如果要使该代码正常工作,则必须使用低于17的版本(即(16.6.2)),或者查找更新为所使用的hapi版本的代码。

const Bcrypt = require('bcrypt');
const Hapi = require('hapi');

const users = {
john: {
    username: 'john',
    password: '$2a$10$iqJSHD.BGr0E2IxQwYgJmeP3NvhPrXAeLSaGCj6IR/XU5QtjVu5Tm',   // 'secret'
    name: 'John Doe',
    id: '2133d32a'
}
};

const validate = async (request, username, password, h) => {

if (username === 'help') {
    return { response: h.redirect('https://hapijs.com/help') };     // custom response
}

const user = users[username];
if (!user) {
    return { credentials: null, isValid: false };
}

const isValid = await Bcrypt.compare(password, user.password);
const credentials = { id: user.id, name: user.name };

return { isValid, credentials };
};

const main = async () => {

const server = Hapi.server({ port: 4000 });

await server.register(require('hapi-auth-basic'));

server.auth.strategy('simple', 'basic', { validate });
server.auth.default('simple');

server.route({
    method: 'GET',
    path: '/',
    handler: function (request, h) {

        return 'welcome';
    }
});

await server.start();

return server;
};

main()
.then((server) => console.log(`Server listening on ${server.info.uri}`))
.catch((err) => {

console.error(err);
process.exit(1);
});

关于node.js - Hapi js API基本身份验证错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48618511/

10-11 11:07