1

[Update] Question now contains necessary details because of which the author was successful at finding a solution and hence should be reopened for answers.

Question in the title. Is there a way to use connect-flash in the back end (express) so that messages are accessible in the front end react app?

My passport local strategy, I am using a sqlite db:

 passport.use(
        new LocalStrategy({ usernameField: 'name' },
        (name, password, done) => {
            //Match user
            db.get(`SELECT * FROM Users WHERE name = '${name}'`, (err, user) => {
                if(err) throw err;
                if(!user) {
                    return done(null, false, { msg : 'name not registered' });
                }
                //Match password
                bcrypt.compare(password, user.password, (err, isMatch) => {
                    if(err) throw Error;
                    if(isMatch) {
                        done(null, user);
                    } else {
                        done(null, false, { msg : 'Password incorrect' })
                    }
                });
            })
        })
    );

and my /login route:

//Login Handle
usersRouter.post('/login', passport.authenticate('local'), (req, res ) => {
    res.json('Successfully authenticated.')
    }
);

I read that I can add options to this route in an object including

{
    failureFlash: true
}

but I am just don't understand how to access the messages (stored in req.flash ?) in my react front end.

BEvo
  • 357
  • 6
  • 18
SirNoob
  • 569
  • 1
  • 4
  • 18
  • yes there is. Can you please share the code where you define your passport strategy? – BEvo Nov 24 '20 at 12:23
  • yes of course! I edited in the question, sorry! – SirNoob Nov 24 '20 at 12:32
  • and also where you use passports authenticate function? – BEvo Nov 24 '20 at 12:42
  • added it as well :D – SirNoob Nov 24 '20 at 13:09
  • http://www.passportjs.org/docs/downloads/html/ please read the verify call back section from this documentation. I would love to help you with code but i think you wish to understand how the damn thing works. – BEvo Nov 24 '20 at 13:15
  • to your update: the problem is, your verify call back signature doesn't match the passport.authenticate functions' arguments that you use as a middle-ware function in your route. In addition to that, you need to do some other config settings related to connect flash, and passing the req object to your strategy callback. So the docs are a good starting point. – BEvo Nov 24 '20 at 13:21
  • The verify call back section was a great tip! I used the template for a custom call back from the documentation and used the "info" argument in it to send the error messages to my React! `usersRouter.post('/login', (req, res, next) => { passport.authenticate('local', (err, user, info) => { if (err) { return next(err); } if (!user) { return res.json(info); } req.logIn(user, (err) => { if (err) { return next(err); } return res.json(info); }); })(req, res, next); });` – SirNoob Nov 24 '20 at 13:56
  • Bravo! Im gonna put that as the answer, could you please accept it? – BEvo Nov 24 '20 at 14:07

0 Answers0