Link to home
Start Free TrialLog in
Avatar of Michael Vasilevsky
Michael VasilevskyFlag for United States of America

asked on

How to Pass Flash Messages to Redirect Controller

I have the following:
//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'
});

Open in new window


My issue is flash messages get lost on redirects to "/". How do I pass the flash messages to findRoute?
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Hi Michael,

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?
Avatar of Michael Vasilevsky

ASKER

Yes sure I'm using connect-flash for flash messages.  

//app.js
const flash = require('connect-flash');

Open in new window


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 });
};

Open in new window


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 TRIAL
Members 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.