hapi-auth-bearer-token: Error: Unknown authentication strategy
I am trying to use this module to enable bearer token on my app. However, I am getting the error shown in the title.
I have been stuck for days now. Hoping anyone could help me.
I have user.js
route
module.exports = [{
'method' : 'GET',
'path' : '/v1/users',
'config' : {
'handler' : function (request, reply) {
User.find(function (error, data) {
if (!error) {
reply(data)
.code(200)
}
});
},
'auth' : 'simple'
}
}];
Now, inside my server.js
,
Server.register([{
'register' : require('hapi-auth-bearer-token')
}, {
'register' : require('./api')
}], function (error) {
if (!error) {
Server.auth.strategy('simple', 'bearer-access-token', {
'validateFunc' : function (token, callback) {
if (token === "1234") {
callback(null, true, {
'token' : token
})
} else {
callback(null, false, {
'token' : token
})
}
}
});
Server.start(function () {
console.log('info', 'Server running at ' + Server.info.uri);
});
}
});
But I get Error: Unknown authentication strategy: simple in path: /v1/users
Complete error message:
at Object.exports.assert (/var/nodevagrant/project/Interest/node_modules/hapi/node_modules/hoek/lib/index.js:663:11)
at /var/nodevagrant/project/Interest/node_modules/hapi/lib/auth.js:139:14
at Array.forEach (native)
at internals.Auth._setupRoute (/var/nodevagrant/project/Interest/node_modules/hapi/lib/auth.js:136:24)
at new module.exports.internals.Route (/var/nodevagrant/project/Interest/node_modules/hapi/lib/route.js:131:47)
at internals.Connection._addRoute (/var/nodevagrant/project/Interest/node_modules/hapi/lib/connection.js:342:17)
at internals.Connection._route (/var/nodevagrant/project/Interest/node_modules/hapi/lib/connection.js:334:18)
at internals.Plugin._apply (/var/nodevagrant/project/Interest/node_modules/hapi/lib/plugin.js:432:14)
at internals.Plugin.route (/var/nodevagrant/project/Interest/node_modules/hapi/lib/plugin.js:407:10)
at Object.exports.register (/var/nodevagrant/project/Interest/api/index.js:8:9)
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 16 (7 by maintainers)
@johnbrett thanks for this, very useful. https://github.com/johnbrett/hapi-level-sample/blob/master/plugins/authentication/index.js
Hey @technowar, here is an example of an authentication plugin, where you can encapsulate all auth related functionality, e.g. scheme registration, strategy function.
Registering the authentication plugin: https://github.com/johnbrett/hapi-level-sample/blob/master/index.js#L7 The auth plugin itself: https://github.com/johnbrett/hapi-level-sample/blob/master/plugins/authentication/index.js
Any questions or feedback on this approach are much welcomed, hope it helps 😃