Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
14 changes: 14 additions & 0 deletions test/auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Loading