Closed wenboSky closed 8 years ago
Hi!
Could a thin wrapper like https://github.com/sb8244/passwordless-hapi help?
Cheers
On 20 Oct 2016, at 09:07, bob notifications@github.com wrote:
Hi guys, Does passwordless work well with Koa instead of Express? I find we cannot make them work together because Koa middleware is a little different from Express, anybody can help? Thanks in advance.
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.
Thanks guy, I tried to wrap each API of passwordless to a Koa version, it's seems that works.
Hi, @florianheinemann, sorry to trouble you.
I used acceptToken successRedirect as following:
app.use(**acceptToken**({successRedirect: '/next'}));
But I found the browser never redirect to /next, it seems no response from server. When I try to trace the code in passwordless.acceptToken, I found there is code logic as following for 'success' callback function:
if(options.successRedirect) {
// next() will not be called, and
// enableOriginRedirect has priority
return self._redirectWithSessionSave(req, res, next, options.successRedirect);
}
Then I simply changed this code as following, everything works well:
if(options.successRedirect) {
// next() will not be called, and
// enableOriginRedirect has priority
res.redirect(options.successRedirect)
;
next();
return;
}
BTW, I used Koa2, so I wrapped passwordless's middleware function to Koa2 version as followings:
function acceptToken(options) {
const middleware = passwordless.acceptToken(options);
return async function koaPasswordless_acceptToken (ctx, next) {
let hasNext = await applyExpressMiddleware(middleware, ctx.request, ctx.response)
if (hasNext && next) {
await next()
}
}
}
Following is the function applyExpressMiddleware which try to wrap a Express middleware to Koa middleware. function applyExpressMiddleware (fn, req, res) { const originalEnd = res.end return new Promise((resolve) => { res.end = function () { originalEnd.apply(this, arguments) resolve(false) } fn(req, res, function () { resolve(true) }) }) }
BTW2, I also wrap other 2 middlewares like requestToken, sessionSupport, all them works well except SuccessRedirect. Could you help to address this issue?
Hi!
Certainly not an expert in Koa. However, you might want to consider that express does expect next or res.redirect but not both. Could it be that the wrapper logic only listens for next but not for a response?
Cheers
I simply install a middleware to call response.redirect(), everything works well. not sure why.
Hi guys, Does passwordless work well with Koa instead of Express? I find we cannot make them work together because Koa middleware is a little different from Express, anybody can help? Thanks in advance.