Hi Artur Shahbazyan,
The problem wherein req.session.user is working perfectly locally but not on the Azure server should probably have to do with session storage. By default, in a local run, sessions are stored in memory, which is great for development. But in production or on Azure App Service, the app tends to be run in a load-balanced or scaled scenario where several instances of the app are created.
In these cases:
In-memory session storage is lost upon restarting or scaling the instance.
All data held in memory, including session data, is lost, so req.session.user is not carried forward on Azure.
You can also install necessary packages
Use a persistent session store or setup Redis in Azure such as Redis, MongoDB, or Azure Table Storage. For instance, with Redis example:
javascript
const session = require('express-session');
const RedisStore = require('connect-redis')(session);
app.use(session({
store: new RedisStore({ host: 'your-redis-host', port: 6379 }),
secret: 'your-secret-key',
resave: false,
saveUninitialized: false,
}));
Also set secure: true only if using HTTP, Use cookie.maxAge
to control session expiration.
https://learn.microsoft.com/en-us/azure/azure-cache-for-redis/cache-nodejs-get-started?tabs=entraid&pivots=azure-managed-redis
https://learn.microsoft.com/en-us/azure/app-service/tutorial-nodejs-mongodb-app?tabs=copilot&pivots=azure-portal
If the answer is helpful, please click Accept Answer and kindly upvote it so that other people who faces similar issue may get benefitted from it.
Let me know if you have any further Queries.