最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - Fastify Auth - Async methods dont finish befor handler called - Stack Overflow

programmeradmin1浏览0评论

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 badges
Add a comment  | 

2 Answers 2

Reset to default 1

It 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
发布评论

评论列表(0)

  1. 暂无评论