2015-07-21 80 views
0

我正在使用hapi-auth-bearer-token插件進行我的api身份驗證,使用hapijs身份驗證策略簡單使用未知方案:承載訪問令牌

這裏是我的代碼:

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

這裏是我得到的錯誤:

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) 

的消息是純英文的,但我不明白我必須添加到我的代碼來解決問題。

回答

2

它看起來像你註冊一個服務器(apiServer)在hapi-auth-bearer-token插件的設置,然後在另一個(server

的權威性戰略嘗試

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