Setup sessions

By default the user sessions are stored on disk. It can be a limitation if you want to scale the application horizontally by adding more Front-Commerce nodes or machines to serve users. This page will explain how to configure alternative session storages through the config/sessions.js configuration file.

Configure sessions via redis

You can handle user session using redis by configuring it in my-module/config/sessions.js.

const session = require("express-session");
const RedisStore = require("connect-redis")(session);
const { createClient } = require("redis");

const redisClient = createClient({
  // see https://www.npmjs.com/package/redis#options-object-properties
  host:
    process.env.FRONT_COMMERCE_CLOUD_REDIS_SESSIONS_HOST ||
    process.env.FRONT_COMMERCE_REDIS_SESSIONS_HOST ||
    "127.0.0.1",
  port:
    process.env.FRONT_COMMERCE_CLOUD_REDIS_SESSIONS_PORT ||
    process.env.FRONT_COMMERCE_REDIS_SESSIONS_PORT ||
    6379,
  db: process.env.FRONT_COMMERCE_REDIS_SESSIONS_DB || 2,
});

module.exports = {
  // see https://developers.front-commerce.com/docs/advanced/production-ready/sessions.html
  store: () => {
    return new RedisStore({ client: redisClient });
  },
};

You will also need to execute npm install connect-redis to install the dependency.

Automatic session configuration

Used in the case when you want your session to be handled by Redis only when it has been configured and is on files otherwise, you can use the following my-module/config/sessions.js file.

const session = require("express-session");
const RedisStore = require("connect-redis")(session);
const { createClient } = require("redis");
const FileStore = require("session-file-store")(session);
const path = require("path");

if (process.env.FRONT_COMMERCE_CLOUD_REDIS_SESSIONS_HOST) {
  const redisClient = createClient({
    // see https://www.npmjs.com/package/redis#options-object-properties
    host: process.env.FRONT_COMMERCE_CLOUD_REDIS_SESSIONS_HOST,
    port: process.env.FRONT_COMMERCE_CLOUD_REDIS_SESSIONS_PORT || 6379,
    db: process.env.FRONT_COMMERCE_REDIS_SESSIONS_DB || 2,
  });

  module.exports = {
    // see https://developers.front-commerce.com/docs/advanced/production-ready/sessions.html
    store: () => {
      return new RedisStore({ client: redisClient });
    },
  };
} else {
  module.exports = {
    // Return your session store implementation here
    store: () => {
      const sessionsPath = path.join(process.cwd(), ".front-commerce/sessions");
      return new FileStore({
        path: sessionsPath,
      });
    },
  };
}
Edit on GitHub  or  Send us a feedback