简体   繁体   中英

Authentication strategy simple uses unknown scheme: bearer-access-token

I am using hapi-auth-bearer-token plugin for my api authentication using hapijs .

here is my code:

apiServer.register(require('hapi-auth-bearer-token'), function (err) {

    server.auth.strategy('simple', 'bearer-access-token', {
        allowQueryToken: true,              // optional, true by default
        allowMultipleHeaders: false,        // optional, false by default
        accessTokenName: 'access_token',    // optional, 'access_token' by default
        validateFunc: function( token, callback ) {

            // For convenience, the request object can be accessed
            // from `this` within validateFunc.
            var request = this;

            // Use a real strategy here,
            // comparing with a token from your database for example
            if(token === "1234"){
                //## user object to be looked up here
                callback(null, true, { token: token })
            } else {
                callback(null, false, { token: token })
            }
        }
    });
});

here is the error i am getting:

Error: Authentication strategy simple uses unknown scheme: bearer-access-token
    at Object.exports.assert (/Users/jamshidnafisi/Documents/srvs-node/node_modules/hapi/node_modules/hoek/lib/index.js:723:11)
    at internals.Auth.strategy (/Users/jamshidnafisi/Documents/srvs-node/node_modules/hapi/lib/auth.js:44:10)
    at internals.Plugin._applyChild (/Users/jamshidnafisi/Documents/srvs-node/node_modules/hapi/lib/plugin.js:471:19)
    at Object.auth.strategy (/Users/jamshidnafisi/Documents/srvs-node/node_modules/hapi/lib/plugin.js:69:18)
    at /Users/jamshidnafisi/Documents/srvs-node/index.js:78:17
    at done (/Users/jamshidnafisi/Documents/srvs-node/node_modules/hapi/node_modules/items/lib/index.js:30:25)
    at Object.exports.register (/Users/jamshidnafisi/Documents/srvs-node/node_modules/hapi-auth-bearer-token/lib/index.js:73:5)
    at /Users/jamshidnafisi/Documents/srvs-node/node_modules/hapi/lib/plugin.js:254:14
    at iterate (/Users/jamshidnafisi/Documents/srvs-node/node_modules/hapi/node_modules/items/lib/index.js:35:13)
    at Object.exports.serial (/Users/jamshidnafisi/Documents/srvs-node/node_modules/hapi/node_modules/items/lib/index.js:38:9)
    at internals.Plugin.register (/Users/jamshidnafisi/Documents/srvs-node/node_modules/hapi/lib/plugin.js:236:11)
    at Object.<anonymous> (/Users/jamshidnafisi/Documents/srvs-node/index.js:76:11)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)

the message is plain english but i dont understand what i have to add to my code to solve the issue.

It looks like you're registering the hapi-auth-bearer-token plugin on one server ( apiServer ) then setting the auth strategy on another ( server )

Try

apiServer.register(require('hapi-auth-bearer-token'), function (err) {

    apiServer.auth.strategy('simple', 'bearer-access-token', {
        allowQueryToken: true,              // optional, true by default
        allowMultipleHeaders: false,        // optional, false by default
        accessTokenName: 'access_token',    // optional, 'access_token' by default
        validateFunc: function( token, callback ) {

            // For convenience, the request object can be accessed
            // from `this` within validateFunc.
            var request = this;

            // Use a real strategy here,
            // comparing with a token from your database for example
            if(token === "1234"){
                //## user object to be looked up here
                callback(null, true, { token: token })
            } else {
                callback(null, false, { token: token })
            }
        }
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM