koa-flash icon indicating copy to clipboard operation
koa-flash copied to clipboard

Compatibility issue with passport/koa-passport

Open joviol opened this issue 9 years ago • 2 comments

Hello,

I tried to use this module in a web app that also uses koa-passport, the problem is that passport expects flash, in the request to be a function accepting two parameters (type and message) so what happens is that passport crashes when it finds this version of flash installed.

joviol avatar Jun 18 '15 18:06 joviol

It turns out that this isn't really compatible with anything that expects the connect-flash package. I'll be submitting a PR of a version I'm using that is compatible soon.

mattlyons0 avatar Mar 07 '17 09:03 mattlyons0

After looking into it, it seems that my solution only works with async/await (or possibly something to do with me using different versions of middleware and koa?). Anyways its not worth me looking into it too deeply as its pretty trivial code. Here is how I maintain compatibility with connect-flash dependants.

app.use(async (ctx,next) => { //Replicate functionality of connect-flash for express (and maintain compatibility with passport)
    if (ctx.session.flash === undefined) {
      ctx.session.flash = {};
    }
    ctx.request.flash = (type, msg) => { //Inject flash function into request
      ctx.session.flash[type] = msg;
    };

    ctx.flash = ctx.session.flash;
    ctx.session.flash = {};

    await next();

    if (ctx.status === 302 && ctx.session && !(ctx.session.flash)) {
      ctx.session.flash = ctx.flash;
    }
  })

I don't use this package at all and instead just use the middleware I wrote above. When I was trying to write a PR I kept running into the issue where the injected function is called and this.session was undefined. Possibly a difference in the Koa@1 lifecycle using this instead of ctx.

mattlyons0 avatar Mar 07 '17 22:03 mattlyons0