Skip to content

Commit

Permalink
bugfix: composite auth OR sequentially (#224)
Browse files Browse the repository at this point in the history
  • Loading branch information
yakovenkodenis authored Feb 26, 2024
1 parent 211676d commit 0305a08
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ function auth (pluginOptions) {
} else {
if (!err && that.options.run !== 'all') {
that.currentError = null
that.completeAuth()
return that.completeAuth()
}
that.nextAuth(err)
}
Expand Down
15 changes: 15 additions & 0 deletions test/example-composited.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ function build (opts) {
}

function verifyOddAsync (request, reply) {
request.verifyOddAsyncCalled = true
return new Promise((resolve, reject) => {
verifyOdd(request, reply, (err) => {
if (err) reject(err)
Expand All @@ -61,6 +62,7 @@ function build (opts) {
}

function verifyBigAsync (request, reply) {
request.verifyBigAsyncCalled = true
return new Promise((resolve, reject) => {
verifyBig(request, reply, (err) => {
if (err) reject(err)
Expand Down Expand Up @@ -152,6 +154,19 @@ function build (opts) {
}
})

fastify.route({
method: 'POST',
url: '/check-two-sub-arrays-or-2',
preHandler: fastify.auth([[fastify.verifyBigAsync], [fastify.verifyOddAsync]], { relation: 'or' }),
handler: (req, reply) => {
req.log.info('Auth route')
reply.send({
verifyBigAsyncCalled: !!req.verifyBigAsyncCalled,
verifyOddAsyncCalled: !!req.verifyOddAsyncCalled
})
}
})

fastify.route({
method: 'POST',
url: '/check-composite-or-async',
Expand Down
20 changes: 20 additions & 0 deletions test/example-composited.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,26 @@ test('Two sub-arrays Or Relation success', t => {
})
})

test('Two sub-arrays Or Relation called sequentially', t => {
t.plan(2)

fastify.inject({
method: 'POST',
url: '/check-two-sub-arrays-or-2',
payload: {
n: 110
}
}, (err, res) => {
t.error(err)
const payload = JSON.parse(res.payload)

t.same(payload, {
verifyBigAsyncCalled: true,
verifyOddAsyncCalled: false
})
})
})

test('Two sub-arrays Or Relation fail', t => {
t.plan(2)

Expand Down

0 comments on commit 0305a08

Please sign in to comment.