hapi-auth-jwt2
Advanced tools
Comparing version 8.4.0 to 8.5.0
@@ -196,7 +196,8 @@ 'use strict'; | ||
try { | ||
let { isValid, credentials, response } = await options.validate( | ||
verify_decoded, | ||
request, | ||
h | ||
); | ||
let { | ||
isValid, | ||
credentials, | ||
response, | ||
errorMessage, | ||
} = await options.validate(verify_decoded, request, h); | ||
if (response !== undefined) { | ||
@@ -208,3 +209,7 @@ return h.response(response).takeover(); | ||
return h.unauthenticated( | ||
raiseError('unauthorized', 'Invalid credentials', tokenType), | ||
raiseError( | ||
'unauthorized', | ||
errorMessage || 'Invalid credentials', | ||
tokenType | ||
), | ||
{ credentials: decoded } | ||
@@ -211,0 +216,0 @@ ); |
{ | ||
"name": "hapi-auth-jwt2", | ||
"version": "8.4.0", | ||
"version": "8.5.0", | ||
"description": "Hapi.js Authentication Plugin/Scheme using JSON Web Tokens (JWT)", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -184,2 +184,3 @@ # Hapi Auth using JSON Web Tokens (JWT) | ||
- `response` - (***optional***) If provided will be used immediately as a takeover response. | ||
- `errorMessage` - (***optional*** *defaults to* `'Invalid credentials'`) - the error message raised to Boom if the token is invalid (passed to `errorFunc` as `errorContext.message`) | ||
@@ -186,0 +187,0 @@ ### *Optional* Parameters |
@@ -20,2 +20,8 @@ const test = require('tape'); | ||
} | ||
if (decoded.id === 139) { | ||
return { isValid: false } | ||
} | ||
if (decoded.id === 140) { | ||
return { isValid: false, errorMessage: 'Bad ID' } | ||
} | ||
return { response: h.redirect('https://dwyl.com') } | ||
@@ -46,4 +52,13 @@ }, | ||
t.equal(response.headers.location, 'https://dwyl.com', 'Server redirect header'); | ||
options.headers.Authorization = JWT.sign({id: 139, name: 'Test'}, secret); | ||
response = await server.inject(options); | ||
t.equal(response.statusCode, 401, 'Server errors when isValid false'); | ||
t.equal(response.result.message, 'Invalid credentials', 'Default error message when custom not provided'); | ||
options.headers.Authorization = JWT.sign({id: 140, name: 'Test'}, secret); | ||
response = await server.inject(options); | ||
t.equal(response.result.message, 'Bad ID', 'Custom error message when provided'); | ||
t.end(); | ||
}); |
143661
2786
687