gpt4 book ai didi

node.js - Hapi js API基本身份验证错误

转载 作者:太空宇宙 更新时间:2023-11-03 23:21:54 25 4
gpt4 key购买 nike

我正在研究 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com