Session middleware

System Information
  • Strapi Version:
  • Operating System:
    Ubuntu 20.04
  • Database:
  • Node Version:
  • NPM Version:
  • Yarn Version:

I’m trying to use session variables and in order to do so I’ve enabled this middleware by configuring /config/middleware.js:

module.exports = {

  settings: {

    logger: {

      level: process.env.NODE_ENV === "production" ? "info" : "debug",


    session: { enabled: true },


  1. What is the recommended way of using it? ctx.state.session.mySession or ctx.session.mySession?
  2. I guess that this explicit code is redundant, right? I mean, just enabling it as above is sufficient.
const session = require("koa-session");
const Koa = require("koa");
const app = new Koa();
  1. Is this the right way of doing it? The reason I’m asking is that on one environment (local) setting a session (either by the 1st example or the 2nd) works as expected while on a different environment (AWS) the values are empty.

Thanks in advance,

Well, one difference I’ve just noticed is the fact that process.env.NODE_ENV is set to production on the AWS environment, wondering it this is the reason for this…

And what really seems to cause the issue is that the session is indicated as isNew upon every request:
isNew: true

Any idea?

Since strapi uses Koa-sessions I would recommend also looking inside it’s documentation/github issues: GitHub - koajs/session: Simple session middleware for koa

As Koa’s documentation says: ctx.session

Yes, just enabling session inside the middleware.js should be sufficient, you should not include koa-session and add the usage of the session manually since strapi already does this job for you.

Does it set a new session on every request?

Can you try to run strapi in production mode on localhost? And see if the same happens on localhost.
NODE_ENV=production yarn start