I have a expressjs server and want to store the session in redis I have my session middleware which looks like the following:
//Importing required packages/files
const session = require('express-session');
const {v4: uuidv4} = require('uuid');
//Redis
const redisStore = require('connect-redis')(session);
const redis = require('redis');
const redisClient = redis.createClient({
host: 'localhost',
port: 6379
});
redisClient.connect().then(() => console.log('Redis connected!'));
//Use Middleware
module.exports = (app) => {
app.use(
session({
store: new redisStore({ client: redisClient }),
secret: [process.env.SIGN_SESSION, process.env.VALIDATE_SESSION],
name: 'sessionId',
genid: function(req) {
return uuidv4();
},
saveUninitialized: false,
resave: false,
cookie: {
httpOnly: true,
sameSite: true,
secure: process.env.ENV === 'prod',
signed: true
}
}),
);
console.log('Middleware loaded! (Session)');
};
It all seems to be working fine.. until I get to the login where I store the user object inside a session variable. Before I switched to redis I used to something like that
const userObj = {
uid: apiResponse.message[0]._id,
username: apiResponse.message[0].username
};
//Set user session object
req.session.user = userObj;
But now after I set the session store to Redis I am not able to set any session variables.. I always get the redis error TypeError: Invalid Argument Type. I already tried to stringify it with JSON or for testing only set the username.. but everytime I try to set a value it throws me an error.