question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

req.flash is not a function

See original GitHub issue

When I configure my login route as following:

router.post('/login', passport.authenticate('local', {
    successRedirect: '/user/logout',
    failureRedirect: '/user/login?wrong',
    failureFlash: true
}));

I got a req.flash is not a function error. And indeed, req.flash is undefined.

PS: don’t take care about URL, it’s just for testing purposes. 😉

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
cymencommented, Oct 28, 2016

I adapted the above to Koa 2:

app.use(function(ctx, next) {
  ctx.flash = function(type, msg) {
    ctx.session.flash = { type: type, message: msg };
  }

  return next();
});
2reactions
DJviolincommented, Apr 9, 2017

I managed to separate flash messages into cookies and exposing under ctx.flash. I used the example boilerplate from my previous post. Because passport.js targeting ctx.session.flash by default, you have to write your own authentication redirects in your router:

// Passport.js custom callback
// Official implementation is here:
// http://passportjs.org/docs#custom-callback
router.post('/auth', async (ctx, next) => {
  await passport.authenticate('local', (err, user, info) => {
    //console.log(`${err}\n${JSON.stringify(user, null, 4)}\n${info}`);
    if (err) { return next(err); }
    if (!user) {
      ctx.flash = {
        type: 'error',
        message: 'Login error!',
      };
      return ctx.redirect('/login');
    }
    ctx.login(user, () => {
      if (err) { return next(err); }
      ctx.flash = {
        type: 'success',
        message: 'Login was succesful!',
      };
      return ctx.redirect('/admin');
    });
  })(ctx, next);
});

This stores a json cookie in a similar object what passport creating by default. The cookies is always deleted after 10 seconds or on the next request. 10 second is defined in the linked implementation, meaning if a request takes longer, than the user can miss the flash, but you can define longer period.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to resolve req.flash is not a function in node js
req.flash is not a function. I have installed connection-flash npm and session and express-session but instead of that it is throwing an ...
Read more >
req.flash is not a function" using passport with nodejs ... - Reddit
javascript - "TypeError: req. flash is not a function" using passport with nodejs, username and password auth .. any ideas? : r/node.
Read more >
Confusing TypeError req.flash is not a function #8 - GitHub
Hi Jared, i've been implementing a REST API and need to implement a login route. I've read the docs and you example here,...
Read more >
[Solved]-Stuck with req.flash is not a function error-node.js
this error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not...
Read more >
Typeerror: Req.Flash Is Not A Function' With App.Use(Flash())
I've read the docs and you example here, but I am receiving an error that I can't identify Confusing TypeError req.flash is not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found