Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 519
Amit Bhattacharjee Avatar asked Sep 07 '25 13:09

Amit Bhattacharjee


1 Answers

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 }*/) { },
};

like image 166
Matthieu GELLE Avatar answered Sep 10 '25 08:09

Matthieu GELLE