admin管理员组文章数量:1357153
i'm trying to setup Fastify Auth in my project . But i think i miss understand something.
This is my route:
fastify.route({
method: 'GET',
url: '/tournaments/:tournamentId/tournamentnominations',
schema: {
tags: ['Tournament Nominations'],
querystring: querystringSchema,
response: {
'2xx': {
type: 'array',
items: { $ref: 'TournamentNomination' },
},
},
},
onRequest: appController.checkDatabase,
preHandler: fastify.auth(
[
fastify.verifyJWT,
[
async (request, reply, done) => {
console.log('check0');
console.log('check1');
done(new Error('you are not authenticated'));
},
async (request, reply, done) => {
console.log('check2');
await delay(2000);
console.log('check3');
done(new Error('you are not authenticated'));
},
],
],
{ relation: 'and' }
),
handler: TournamentNominationsController.getAll,
preSerialization: manipulate,
});
and this is the console output
check0
check1
check2
te123
[14:07:35.814] INFO (24573): request completed
reqId: "req-5"
res: {
"statusCode": 200
}
responseTime: 12.27206403017044
check3
the "te123" output comes from the handler. But i think the setup the correct logic from the documentation with 'and' and 'or' fastify.auth([f1, f2, [f3, f4]], { relation: 'and' }) f1 AND f2 AND (f3 OR f4). The handler should not be called.
i'm trying to setup Fastify Auth in my project https://github/fastify/fastify-auth. But i think i miss understand something.
This is my route:
fastify.route({
method: 'GET',
url: '/tournaments/:tournamentId/tournamentnominations',
schema: {
tags: ['Tournament Nominations'],
querystring: querystringSchema,
response: {
'2xx': {
type: 'array',
items: { $ref: 'TournamentNomination' },
},
},
},
onRequest: appController.checkDatabase,
preHandler: fastify.auth(
[
fastify.verifyJWT,
[
async (request, reply, done) => {
console.log('check0');
console.log('check1');
done(new Error('you are not authenticated'));
},
async (request, reply, done) => {
console.log('check2');
await delay(2000);
console.log('check3');
done(new Error('you are not authenticated'));
},
],
],
{ relation: 'and' }
),
handler: TournamentNominationsController.getAll,
preSerialization: manipulate,
});
and this is the console output
check0
check1
check2
te123
[14:07:35.814] INFO (24573): request completed
reqId: "req-5"
res: {
"statusCode": 200
}
responseTime: 12.27206403017044
check3
the "te123" output comes from the handler. But i think the setup the correct logic from the documentation with 'and' and 'or' fastify.auth([f1, f2, [f3, f4]], { relation: 'and' }) f1 AND f2 AND (f3 OR f4). The handler should not be called.
Share Improve this question edited Mar 30 at 14:20 wiifree asked Mar 30 at 14:11 wiifreewiifree 1051 silver badge11 bronze badges2 Answers
Reset to default 1It was a problem with the callback of async methods.
Sync methods can run done(new Error('you are not authenticated'));
but async methods must call throw new Error('you are not authenticated');
. The done parameter is not useful here.
In Fastify, you cannot mix async functions with callback-based.
Choose one approach:
❌ Incorrect (Mixed async & callback)
async (request, reply, done) => {
console.log('check0');
console.log('check1');
done(new Error('you are not authenticated'));
}
✅ Correct (Async function should throw
an error)
async (request, reply) => {
console.log('check0');
console.log('check1');
throw new Error('you are not authenticated');
}
✅ Correct (Callback function should use done(err)
)
function (request, reply, done) {
console.log('check0');
console.log('check1');
done(new Error('you are not authenticated'));
}
This rule applies to handlers, hooks, and any function parameters.
More Info
- Fastify Docs: Routes
- Fastify Docs: Reply
本文标签: nodejsFastify AuthAsync methods dont finish befor handler calledStack Overflow
版权声明:本文标题:node.js - Fastify Auth - Async methods dont finish befor handler called - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743986315a2571331.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论