florianheinemann / passwordless

node.js/express module to authenticate users without password
MIT License
1.95k stars 129 forks source link

passwordless with Koa js #84

Closed wenboSky closed 8 years ago

wenboSky commented 8 years ago

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.

florianheinemann commented 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.

wenboSky commented 8 years ago

Thanks guy, I tried to wrap each API of passwordless to a Koa version, it's seems that works.

wenboSky commented 8 years ago

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?

florianheinemann commented 8 years ago

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

wenboSky commented 8 years ago

I simply install a middleware to call response.redirect(), everything works well. not sure why.