rkusa/koa-passport

Local Stragegy error: "TypeError: next is not a function"

maotora opened this issue ยท 6 comments

Hi, awesome middleware!

I'm getting that error when i use local-strategy with this middleware.

The examples work perfectly but i get these errors when i use it by myself.
Here is my code.

//- In passport.js file
import passport from 'koa-passport';
import { Strategy as LocalStrategy } from 'passport-local';

//- Simplified Local strategy...
const localLogin = new LocalStrategy({}, (username, password, done) => {
    User.findOne({username, password}, done);
});

//...Then later
passport.use(localLogin);
//- In my app.js file

const app = new koa(); 

app.use(convert(body({
    multipart: true,
})));

app.use(authApi.middleware());
app.use(api.middleware());

app.use(convert(passport.initialize()));
app.use(convert(passport.session()));

export default app;

and in my routes i have ..

//- Routes file.

import Router from 'koa-rest-router';
import { signIn, signUp } from './../controller/authentication';
import passport from 'passport';

const requireAuth = passport.authenticate('jwt', { session: false });
const requireSignIn = passport.authenticate('local', { session: false });

router.post('/login', requireSignIn, signIn);

The requireSignIn is the one called first and signIn won't even get called when that error fires!

what's interesting is i've tried to replace local in

const requireSignIn = passport.authenticate('local', { session: false });

with

const requireSignIn = passport.authenticate('whatever', { session: false });

and i get the very same error message!

It's like my local-strategy won't get recognized for some reason.

Stack Trace

 TypeError: next is not a function
    at attempt (/home/user/Projects/koa/test-project1/node_modules/passport/lib/middleware/authenticate.js:173:32)
    at authenticate (/home/user/Projects/koa/test-project1/node_modules/passport/lib/middleware/authenticate.js:349:7)
    at dispatch (/home/user/Projects/koa/test-project1/node_modules/koa-compose/index.js:44:32)
    at /home/user/Projects/koa/test-project1/node_modules/koa-compose/index.js:36:12
    at /home/user/Projects/koa/test-project1/node_modules/koa-better-router/index.js:544:46
    at dispatch (/home/user/Projects/koa/test-project1/node_modules/koa-compose/index.js:44:32)
    at next (/home/user/Projects/koa/test-project1/node_modules/koa-compose/index.js:45:18)
    at createGenerator (/home/user/Projects/koa/test-project1/node_modules/koa-convert/index.js:24:16)
    at createGenerator.next (<anonymous>)
    at Function.parseBody (/home/user/Projects/koa/test-project1/node_modules/koa-better-body/utils.js:269:20)
    at parseBody.next (<anonymous>)
    at Object.plugin (/home/user/Projects/koa/test-project1/node_modules/koa-better-body/index.js:50:21)
    at plugin.next (<anonymous>)
    at onFulfilled (/home/user/Projects/koa/test-project1/node_modules/co/index.js:65:19)
    at process._tickCallback (internal/process/next_tick.js:103:7)

Thank You.

rkusa commented

Hi, your stack trace looks like the error originates from koa-better-body and not form koa-passport, which is probably this line:

app.use(convert(body({
    multipart: true,
})));

@rkusa I removed the whole app.use(body()) statement, but i still receive the error.

Here is the message

 TypeError: next is not a function
    at attempt (/home/user/Projects/koa/test-project1/node_modules/passport/lib/middleware/authenticate.js:173:32)
    at authenticate (/home/user/Projects/koa/test-project1/node_modules/passport/lib/middleware/authenticate.js:349:7)
    at dispatch (/home/user/Projects/koa/test-project1/node_modules/koa-compose/index.js:44:32)
    at /home/user/Projects/koa/test-project1/node_modules/koa-compose/index.js:36:12
    at /home/user/Projects/koa/test-project1/node_modules/koa-better-router/index.js:544:46
    at dispatch (/home/user/Projects/koa/test-project1/node_modules/koa-compose/index.js:44:32)
    at /home/user/Projects/koa/test-project1/node_modules/koa-compose/index.js:36:12
    at Server.<anonymous> (/home/user/Projects/koa/test-project1/node_modules/koa/lib/application.js:135:7)
    at emitTwo (events.js:106:13)
    at Server.emit (events.js:191:7)
    at HTTPParser.parserOnIncoming [as onIncoming] (_http_server.js:547:12)
    at HTTPParser.parserOnHeadersComplete(_http_common.js:99:23)

I think i also forgot to mention that when i requireSignIn from the route it goes to the next handler and no such error is emited.

const requireSignIn = passport.authenticate('local', { session: false });

router.post('/login', signIn); //- removing requireSignIn, handler signIn gets called!

Something must be wrong in my codebase.

I rewrote everything and it now works..

Thank you for your help. I will update if i understand the problem.

tiaod commented

@maotora Well, the reason is you require the passport lib instead of koa-passport. ๐Ÿ˜…
I had the same typo too.....

tiaod commented
import passport from 'passport'
app.use(passport.initialize())
app.use(passport.session())

Will throw the flowing error:

  TypeError: next is not a function
      at initialize (/home/xmrg/workspace/indwar/server/node_modules/passport/lib/middleware/initialize.js:53:5)
      at dispatch (/home/xmrg/workspace/indwar/server/node_modules/koa-compose/index.js:44:32)
      at next (/home/xmrg/workspace/indwar/server/node_modules/koa-compose/index.js:45:18)
      at createGenerator (/home/xmrg/workspace/indwar/server/node_modules/koa-convert/index.js:24:16)
      at createGenerator.next (<anonymous>)
      at onFulfilled (/home/xmrg/workspace/indwar/server/node_modules/co/index.js:65:19)
      at /home/xmrg/workspace/indwar/server/node_modules/co/index.js:54:5
      at Object.co (/home/xmrg/workspace/indwar/server/node_modules/co/index.js:50:10)
      at Object.toPromise (/home/xmrg/workspace/indwar/server/node_modules/co/index.js:118:63)
      at next (/home/xmrg/workspace/indwar/server/node_modules/co/index.js:99:29)

You should change import passport from 'passport' to import passport from 'koa-passport'
image

@tiaod lol Thank You, You are right, when I rewrote everything it worked!

I must have import'ed 'koa-passport' the second time and I didn't know what solved that.

Good Catch!