Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to config Redis as session storage with KrakenJS

With Express, I can use Redis as session storage like this:

var express = require('express');
var app = express();
var RedisStore = require('connect-redis')(express);

app.use(express.cookieParser());
app.use(express.session({
    store: new RedisStore({
    host: 'localhost',
    port: 6379,
    db: 'test',
    pass: '123456'
  }),
  secret: '123456789'
}));

But with Kraken, the session configuration is in the file config/middleware.json. I changed the file to use connect-redis as following:

{
    "middleware": {
        "session": {
           "module": "connect-redis",
           "secret": "99b91c387e6e049308fc31d3cfff5fd3149e419c"
        }
    }
}

This way, Kraken does use Redis as session storage but how do I suppose to pass Redis' options (password, host, db name...) like with Express?

like image 286
Linh Tran Avatar asked Dec 12 '22 09:12

Linh Tran


2 Answers

The documentation for kraken-js is not very extensive. Hope it improves.

We had to dig into the code and understand the logic and we were able to solve.

Hope this help others who want to use Redis Session store with Kraken.js

Add "connect-redis" to package.json

"dependencies": {
    ...
    "connect-redis": ">=1.0.0"
},

Install "connect-redis" module

npm install

In the config/middleware.json add the config as below

{
    "middleware": {
        "session": {
            "module": "connect-redis",
            "config": {
                "host": "localhost",
                "port": 6379,
                "db": 1
            },
            "secret": "a1df0e81ef54d199567befb02761b1834c8b7406"
        }
    }
}

Change the config as required

Start/Restart server and it should work!

Provided you already have Redis running

like image 200
Sudhir Kumar Avatar answered Jan 06 '23 00:01

Sudhir Kumar


A complete working example can be found here: https://github.com/AlexSantos/Kraken_Example_Session_Redis

like image 23
Lenny Markus Avatar answered Jan 06 '23 01:01

Lenny Markus