Skip to content
Advertisement

Koa & Passport Missing credentials

I have a Koa server that uses Passport to authenticate users against an Array, and a React client. After successful login, the following requests are not authenticated as the cookie is undefined. The authenticate function’s error parameter has:

JavaScript

After browsing the site, I fixed the usual errors, calling the returned function of authenticate, adding {credentials: 'include'} to fetch etc, but still I have the same problem.

Middleware list: router.use(cookie.default());

app.use :

koa-body, koa-session-store (also tried with koa-session), passport.initialize(), passport.session(), router.routes(), koa-static

local strategy

JavaScript

/login authenticate

JavaScript

/login-success

JavaScript

Client call

JavaScript

Advertisement

Answer

The fix is actually simple, but the reason is hard to find.

async middleware must either call await next() or return next() at the end. Otherwise a 404 error is caused.

Adding await next() to the async /login-success callback, fixed the issue.

Documentation: https://github.com/koajs/koa/blob/master/docs/troubleshooting.md#my-middleware-is-not-called

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement