question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
tiaodcommented, Mar 2, 2017

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

1reaction
maotoracommented, Jan 3, 2017

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Node js Next is not a function in passport strategy error
Can't seem to get a login system working. I'm using a json file with the user information, and am using node.js, express and...
Read more >
next is not a function TypeError: next is not a function #3407
Describe the bug. After cloning the repo and running docker-compose up , I'm getting the following error message when clicking on log in...
Read more >
JavaScript: Uncaught TypeError: n is not a function
This error occurs if you try to execute a function that is not initialized or is not initialized correctly. This means that the...
Read more >
What went wrong? Troubleshooting JavaScript - MDN Web Docs
An error message to indicate what's gone wrong: "TypeError: guessSubmit.addeventListener is not a function"; A "Learn More" link that links ...
Read more >
Authenticate Users With Node ExpressJS and Passport.js
Many Node.js applications require users to authenticate in order to access private content. The authentication process must be both functional and secure, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found