'Strapi v4 Grant middleware error while setting providers

In Strapi v4, I tried to setup discord oath provider following the latest-docs. I've setup the keys and everything but when I try to hit /api/connect/discord I'm getting this error

Grant: mount session middleware first

I goggled a bit and found that Strapi need to use grant as a middleware first in order to use it. so I checked /config/middleware.js

module.exports = [
  'strapi::errors',
  'strapi::security',
  'strapi::cors',
  'strapi::poweredBy',
  'strapi::logger',
  'strapi::query',
  'strapi::body',
  'strapi::favicon',
  'strapi::public',
];

it was missing grant so I tried creating a custom global grant middleware but I need to get hold of the app instance and do the following

app.use(session({secret:'grant'));
app.use(grant);

how can I achieve this? or if anyone has any idea to fix the above problem ?



Solution 1:[1]

I had exactly the same concern for the integration of twitch.

The only solution I have found for the moment is to do this, but I hope that there will be a fix in the next update because suddenly we are dependent on the koa-session2 library.

https://github.com/Secbone/koa-session2

=> your-strapi-app/src/index.js

'use strict';

const session = require("koa-session2");

module.exports = {
  /**
   * An asynchronous register function that runs before
   * your application is initialized.
   *
   * This gives you an opportunity to extend code.
   */
  register({ strapi }) {
    strapi.server.use(session({
      secret: "grant",
    }));
  },

  /**
   * An asynchronous bootstrap function that runs before
   * your application gets started.
   *
   * This gives you an opportunity to set up your data model,
   * run jobs, or perform some special logic.
   */
  bootstrap(/*{ strapi }*/) { },
};

Solution 2:[2]

This issue has been resolved in Strapi 4.0.6, and they have provided a migration guide with steps to configure the session middleware.

https://docs.strapi.io/developer-docs/latest/update-migration-guides/migration-guides/v4/migration-guide-4.0.x-to4.0.6.html

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Matthieu GELLE
Solution 2 Dae314