Michael Vasilevsky
asked on
How to Pass Flash Messages to Redirect Controller
I have the following:
My issue is flash messages get lost on redirects to "/". How do I pass the flash messages to findRoute?
//index.js
router.get('/', catchErrors(deviceController.findRoute));
//deviceController.js
exports.findRoute = async (req, res) => {
if (req.user) {
const devices = await Device.find({ owner: req.user._id });
if (!devices.length) {
res.redirect('/store');
} else {
res.redirect('/devices');
}
} else {
res.redirect('/store');
}
}
//authController.js
exports.login = passport.authenticate('local', {
failureRedirect: '/login',
failureFlash: 'Failed Login',
successRedirect: '/',
successFlash: 'You are now logged in'
});
My issue is flash messages get lost on redirects to "/". How do I pass the flash messages to findRoute?
ASKER
Yes sure I'm using connect-flash for flash messages.
If I route to function with a res.render I get my flash message no problem, e.g.:
The problem is they are lost on the .findRoute function, because of the redirects I guess. So my question is if it's possible to pass those flash messages along and how it would like.
Let me know if I can clarify further!
//app.js
const flash = require('connect-flash');
If I route to function with a res.render I get my flash message no problem, e.g.:
exports.getModels = async (req, res) => {
...
res.render('models', { title: 'Models', models, page, pages, count });
};
The problem is they are lost on the .findRoute function, because of the redirects I guess. So my question is if it's possible to pass those flash messages along and how it would like.
Let me know if I can clarify further!
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Can you explain this a bit better. What is a flash message ( I know what it is but in the context of your app) - how does it fit into the code - what is it you are wanting to do?