Skip to main content
Version: 2.x

By default in Front-Commerce, 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 guide explains how to configure alternative session storage.

Sessions storage configuration occurs in the config/sessions.js configuration file. Front-Commerce uses the express-session library and supports any compatible store.

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_CLOUD_REDIS_SESSIONS_DB ||
process.env.FRONT_COMMERCE_REDIS_SESSIONS_DB ||
2,
});

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

You will also need to execute npm install connect-redis@6 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/2.x/advanced/production-ready/sessions
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,
});
},
};
}