koa-flash
koa-flash copied to clipboard
Compatibility issue with passport/koa-passport
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.
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.
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.