I'm storing user sessions in mongo collection, using express-session middleware.
Which is the best way, to update all user sessions, when change performed from one of them.
For example, if user changes email from session A, I'm updating it instantly. But changes don't apply to the rest.
For clarity: Browser 1 - User changed email Browser 2 - Reload page, still getting old data
app.get('/change-email', async (req, res, next) => {
const userData = await User.findOneAndUpdate({ _id: req.session.user.id }, {
$set: {
email: req.body.email,
},
}, {
new: true,
});
req.session.user = {
id: userData._id,
email: userData.email,
username: userData.username,
name: userData.name,
};
});
I expect the change performed from session A, will be distributed to the rest of the user sessions.
As one of the solutions to the problem, I think to search for all user sessions and update them on every user data (email, username, name, etc.) change.