diff --git a/auth.js b/auth.js index 298f7b6..607fb26 100644 --- a/auth.js +++ b/auth.js @@ -8,11 +8,13 @@ const DEFAULT_RELATION = 'or' function fastifyAuth (fastify, opts, next) { if (opts.defaultRelation && opts.defaultRelation !== 'or' && opts.defaultRelation !== 'and') { return next(new Error("The value of default relation should be one of ['or', 'and']")) - } else if (!opts.defaultRelation) { - opts.defaultRelation = DEFAULT_RELATION } - fastify.decorate('auth', auth(opts)) + const pluginOptions = { + defaultRelation: opts.defaultRelation || DEFAULT_RELATION + } + + fastify.decorate('auth', auth(pluginOptions)) next() } diff --git a/test/auth.test.js b/test/auth.test.js index d0d63b5..867b61c 100644 --- a/test/auth.test.js +++ b/test/auth.test.js @@ -17,6 +17,20 @@ test('registering plugin with invalid default relation', (t, done) => { }) }) +test('registering plugin should not mutate opts', (t, done) => { + t.plan(2) + + const fastify = Fastify() + const opts = {} + fastify.register(fastifyAuth, opts) + + fastify.ready((err) => { + t.assert.ifError(err) + t.assert.strictEqual(opts.defaultRelation, undefined) + done() + }) +}) + test('Clean status code through auth pipeline', (t, done) => { t.plan(3)