React App with Express Backend & express-session React App with Express Backend & express-session node.js node.js

React App with Express Backend & express-session


Your axios request from your React client needs to be sent withCredentials. To fix it, either do axios.defaults.withCredentials = true; or do axios.get('url', {withCredentials: true})...Also in your expressjs backend, set cors options credentials: true


Here is an example of setting up express-session using connect-redis. First, setup both express-session and the Redis store.

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

Then, where you're declaring middleware for your app;

app.use(session({  store: new RedisStore({    url: process.env.REDIS_URL  }),  secret: process.env.REDISSECRET,  resave: false,  saveUninitialized: false}));

now when you use req.session.{etc} this writes to your Redis DB.