Add a webhook endpoint to AdonisJS web app | FIXED

@hexacker
@hexacker Adocasts Plus

I'm just getting started with AdonisJS, I used the web app template, and now I need to add a webhook endpoint, so I can receive events from third-party platforms(such as payments) how can I do that.

Excuse my newbie question.

UPDATE

Fixed it, it was just an issue of the Shield middleware.

Create a free account to join in on the discussion
  1. @tomgobich

    Glad to hear you were able to get it figured out, Hexacker!

    For anyone else who may come across this, you want to make sure you have CSRF disabled for the route, which is in config/shield.ts:

    /**
     * Configure CSRF protection options. Refer documentation
     * to learn more
     */
    csrf: {
      enabled: env.get('NODE_ENV') !== 'test',
      exceptRoutes: ['/stripe/webhook'], // 👈 add your webhook route(s) here
      enableXsrfCookie: true,
      methods: ['POST', 'PUT', 'PATCH', 'DELETE'],
    },
    Copied!
    1
    1. Responding to tomgobich
      @hexacker

      That was a better solution than mine.

      Thank you for the amazing content you're sharing 😊 because of you I'm switching from NestJS to AdonisJS 😎.

      1
      1. Responding to hexacker
        @tomgobich

        Thank you for watching!! 😊 That's awesome to hear, thanks Hexacker!! I hope you enjoy your time with AdonisJS!! 😁

        1
  2. @hexacker

    Hi again,

    How I can add a dynamic exception to the shield config. Currently, it looks like this:

      csrf: {
        enabled: true,
        exceptRoutes: ['/webhook', '/users', '/subscription'],
        enableXsrfCookie: true,
        methods: ['POST', 'PUT', 'PATCH', 'DELETE'],
      },`
    Copied!

    The webhook endpoint must looks something like this:

    /webhook/verify/:provider/:identifier

    How can I achieve that

    ____________

    #Update:

    I have fixed it like this

        exceptRoutes: (ctx) => {
          const url = ctx.request.url()
          const staticRoutes = ['/users', '/subscription']
          const dynamicPatterns = [/^\/webhook\/verify\/[^/]+\/[^/]+$/]
          const isExcluded =
            staticRoutes.some((route) => url.startsWith(route)) ||
            dynamicPatterns.some((regex) => regex.test(url))
    
          return isExcluded
        },
    Copied!

    Is there a better way for solving this?

    0
New Discussion
Topic