Reputation: 1525
This is my code now the problem here is when I am running this with hapi version 14.x.x or below it works for me perfectly fine but when I am running this with hapi version 15.0.1 or greater it gives me following error.
Is there any change in authentication process?
throw new Error(msgs.join(' ') || 'Unknown error');
^
Error: Unknown authentication strategy UserAuth in /api/user/loginDetails
at Object.exports.unique.exports.contain.exports.reachTemplate.exports.assert.condition [as assert] (F:\cbl\projects\lawn-mower\node_modules\hapi\node_modules\hoek\lib\index.js:736
:11)
at internals.Auth.test.internals.Auth._setupRoute (F:\cbl\projects\lawn-mower\node_modules\hapi\lib\auth.js:144:14)
Sample route that I am using
{
method: 'POST',
path: '/api/user/loginDetails',
config: {
auth: 'UserAuth',
handler: function (request, reply) {
},
validate: {
payload: {
},
headers: UniversalFunctions.authorizationHeaderObj,
failAction: UniversalFunctions.failActionFunction
},
plugins: {
'hapi-swagger': {
payloadType : 'form',
responses:Config.APP_CONSTANTS.swaggerDefaultResponseMessages
}
}
}
}
strategy I am using
server.register(require('hapi-auth-bearer-token'), function (err) {
console.log("aaaaa");
server.auth.strategy('UserAuth', 'bearer-access-token', {
allowQueryToken: false,
allowMultipleHeaders: true,
accessTokenName: 'accessToken',
validateFunc: function (token, callback) {
}
});
});
Upvotes: 2
Views: 1639
Reputation: 461
this error occurs because you want to register a route that requires an authentication strategy which isn't available to the hapi server yet.
Make sure that the UserAuth
strategy is registered before the route gets added to the server.
This tutorial proposes 2 solutions
Hope that helps!
Upvotes: 1
Reputation: 9933
You have only defined strategy, not scheme. So define your scheme like below code .
You should try this:
var userFunc=function (server, options) {
return {
authenticate: function (request, reply) {
console.log('UserAuth');
return reply.continue({ credentials: { user: 'UserAuth' } });
}
};
};
server.auth.scheme('UserAuthScheme', userFunc); // here
server.auth.strategy('UserAuth', 'UserAuthScheme'); // here
server.route([{
method: 'POST',
path: '/api/user/loginDetails',
config: {
auth: 'UserAuth',
handler: function (request, reply) {
reply('/api/user/loginDetails');
},
validate: {
payload: {
},
headers: UniversalFunctions.authorizationHeaderObj,
failAction: UniversalFunctions.failActionFunction
},
plugins: {
'hapi-swagger': {
payloadType: 'form',
responses: Config.APP_CONSTANTS.swaggerDefaultResponseMessages
}
}
}
}]);
Upvotes: 0
Reputation: 1525
This resolved the issue for me plugin registrations are asynchronous from hapi version 15.x.x or above
Explanation : In version 14.x.x or lower things go synchronously like when we will start the server it will find register the plugins first then it will go for routes server.route(Routes).
This was fixed in hapi version 15.x.x and above now plugin registrations are asynchronous so we need to register our plugins first then need to initialize the routes as in example below.
Click here to get more details about it from latest updates thread no is 3295
server.register(Plugins, function (err) {
if (err){
console.log("===========err=========",err)
server.error('Error while loading plugins : ' + err)
}else {
console.log("====================");
server.route(Routes);
server.log('info','Plugins Loaded');
}
});
Upvotes: 0