How do you access the messages in the done methods of passport without flash / in a react app?

400 Views Asked by At

[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.

0

There are 0 best solutions below