0

newbie with Passport.js here trying to do a simple thing. I just want to send a custom message to my front-end, im using "passport": "^0.4.0", and "express": "^4.17.0", and "passport-local": "^1.0.0", but i cant seem to get it working. I saw something about this being different with express 4, tried to download connect-flash but didnt manage to get it working.

This sends a message with error normally:

res.status(401).send({
    message: 'Authentication key expired'
});

Now im trying to do this with passport strategy, but doesnt seem to send anything to the client but it sends a generic 401 with no custom message:

  passport.use(
    new LocalStrategy({usernameField: "login", passwordField: "password"}, (login, password, done) => {
      User.findByLogin(login).then(async user => {
        if (!user) {
          return done(null, false, { message: 'Incorrect username.' });
        }
        if (!user.validPassword(password)) {
          return done(null, false, { message: 'Incorrect password.' });
        }
        let subscription = await user.getValidSubscription();
        if (!subscription) {
          return done(null, false, {
            message: 'Your subscription has expired.'
          });
        }
        user.dataValues.expiration = subscription.expiration;
        return done(null, user);
      });
    })
  );

Again, im aware that this is a problem with flash messages and stuff, but isnt there an easy way out just to send a custom message? All i need is that the 'Incorrect username' reaches my front-end. Ive spent some days into this and still nothing. Thank you in advance!

Edit1: Made it clearer.

LaioZatt
  • 23
  • 1
  • 5

1 Answers1

1

Try to install flash with npm install connect-flash

After that go to your app.js file and write these:

var flash = require('connect-flash'); app.use(flash())

Inside your passport function add this passReqToCallback

{ usernameField : 'login', passwordField : 'password', passReqToCallback : true }// allows us to pass back the entire request to the callback

And then inside your parameters (login, password, done) you can pass the request object like this (req,login, password, done)

Finally you can send message like this

if (!user.validPassword(password)) { return done(null, false, req.flash('signupMessage', 'Incorrect username.')); }

  • Hello and thanks for the help, did what u said and got the error: Unhandled rejection Error: req.flash() requires sessions at IncomingMessage._flash [as flash] (c:\Users\R2\Documents\Projects\server- eusaudavel\node_modules\connect-flash\lib\flash.js:60:41) at User.findByLogin.then (c:\Users\R2\Documents\Projects\server- eusaudavel\routes\passport.js:31:40) at tryCatcher (c:\Users\R2\Documents\Projects\server- eusaudavel\node_modules\bluebird\js\release\util.js:16:23) ... – LaioZatt Nov 20 '19 at 22:28
  • Also, if i try this (from the accepted answer): https://stackoverflow.com/questions/30563321/error-req-flash-requires-sessions i get: NodeError: Invalid status code: 1 at ServerResponse.writeHead (_http_server.js:209:11) at ServerResponse.writeHead (c:\Users\R2\Documents\Projects\server-eusaudavel\node_modules\on-headers\index.js:44:26) at ServerResponse._implicitHeader (_http_server.js:200:8)... – LaioZatt Nov 20 '19 at 22:38
  • Using return done(null, false, req.flash('Incorrect password.')); instead of return done(null, false, req.flash('signupMessage', 'Incorrect username.')); doesnt give me invalid status code: 1, but it still only gives a generic 401 response – LaioZatt Nov 20 '19 at 23:29