代码之家  ›  专栏  ›  技术社区  ›  Ramitha Rupasinghe

Hapi js API基本身份验证错误

  •  1
  • Ramitha Rupasinghe  · 技术社区  · 7 年前

    我在研究hapi js API基本身份验证,并使用hapi文档进行身份验证。我相信我做的每件事都是对的,但我遇到了以下关于未处理PromisejectionWarning的错误。请帮忙

    指数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);
    });
    });
    

    包裹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.
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   Anto S    7 年前

    如果您希望该代码正常工作,则必须使用低于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);
    });