Preserve original auth error

node v14.20.1
version: master
endpointsharetweet
const Cookie = require('@hapi/cookie'); const Hapi = require('@hapi/hapi'); const Boom = require('@hapi/boom'); const server = Hapi.server(); await server.register(Cookie); server.auth.strategy('session', 'cookie', { cookie: { password: 'secret-secret-secret-secret-secret' }, validateFunc: () => { throw Boom.forbidden('Bad region!'); } }); // This extension is preserves the original auth error: server.ext({ type: 'onPreResponse', method: (request, h) => { const error = request.response; if (Boom.isBoom(error) && error.output.statusCode === 401 && error.data instanceof Error) { // Preserve original error from Boom.unauthorized() return error.data; } return h.continue; } }); server.route({ method: 'post', path: '/login', handler: (request) => { request.cookieAuth.set({ id: 'x' }); return null; } }); server.route({ method: 'get', path: '/auth', handler: () => ({ success: true }), options: { auth: 'session' } }); await server.initialize(); const { headers } = await server.inject({ method: 'post', url: '/login' }); const { result } = await server.inject({ method: 'get', url: '/auth', headers: { cookie: 'sid=' + headers['set-cookie'][0].match(/sid=(.+?);/)[1] } }); console.log(result);
Loading…

no comments

    sign in to comment