1

I built a website with Nuxt and Strapi. I added a cart system using the ctx.session. It works well on local, but when in prod, the session can't be retrived when using Chrome or Safari. But it's perdect with Firefox.

I logged to see what's happening and it seems that the sessions is never stored. After an action is done, nothing remains.

Here is my middleware.js :

const isProd = process.env.NODE_ENV === 'production'

module.exports = {
  //...
  settings: {
    cors: {
      enabled: true, 
      // headers: '*', 
      credentials: true,
      origin: isProd 
        ? ['https://xxxxxx.com', 'https://yyyyy.xxxxxx.com']
        : ['http://localhost:3000', 'http://localhost:1337']
  
    },
    logger: {
      level: 'trace'
    }
  },
}

and my server.js :

module.exports = ({ env }) => ({
  host: env('HOST', '0.0.0.0'),
  port: env.int('PORT', 1337),
  admin: {
    auth: {
      secret: env('ADMIN_JWT_SECRET', 'XXXXXXXXXXXX'),
    },
  },
  cron: { enabled: true }
});

On the front side, here is my Axios config :

const apiClient = axios.create({
  baseURL: `${process.env.baseUrl}/`,
  withCredentials: true,
  headers: {
    Accept: 'application/json',
    'Content-Type': 'application/json',
  },
  timeout: 10000,
})

Thank you

Antro
  • 43
  • 5
  • 1
    What is `ctx.session`? [Here is](https://stackoverflow.com/a/67705541/8816585) how to properly use env variables with Nuxt. Otherwise, why do you have some CORS in your Nuxt? Isn't this supposed to be on the server? Did you tried in incognito tabs on all the browsers? Cleaned all the cookies/localStorage etc on all of them? Strange that it works on Firefox I'd have to say. – kissu Apr 18 '22 at 16:22
  • `ctx.session` is provided by Strapi, using Koa ([see here](https://koajs.com/#context). I tried incognito, cleaned everything, other computer, nothing works... – Antro Apr 19 '22 at 08:56

1 Answers1

1

I finally found the solution!

I was missing the session activation in the middleware.js config file.

module.exports = {
    //...
    settings: {
        ...otherSettings,
        session: { enabled: true }
    },
}
Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
Antro
  • 43
  • 5