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.

async/await passport-jwt custom handler

See original GitHub issue

Hi, I am using koa-router and koa-passport with passport-jwt. The problem is that every error thrown inside passport custom callback is not catched by parent middleware. Am I missing something or is this intended behaviour? Can you please provide example how this should work with async/await and koa2?

Best regards

privateRouter.get('/test', isAuthenticated, users.test)

async function authenticate(ctx, next) {
  await passport.authenticate('jwt', { session: false }, async(user, userInfo) => {
       await next() // when next middleware throws error it won't be catched by parent middleware
       throw new Error() // this won't be catched by parent middleware
  })(ctx, next)
}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
krutijan1commented, Jul 25, 2016

I understand, I have created minimal working example demonstrating issue here. Maybe later on I can provide pull request with suggested changes, when I find time to dig deeper. Meanwhile I am using this workaround if it can help anyone:

  async function authenticate(ctx, next) {
    await passport.authenticate('jwt', { session: false }, async(user, userInfo) => {
        try {
          await next() // when next middleware throws error it won't be catched by parent middleware
         } catch (e) {
            // set error response directly here to stop propagating upstream
            ctx.status = e.status
            cts.message = e.message
         }
    })(ctx, next)
  }
1reaction
krutijan1commented, Jul 26, 2016

I have incorporated your fix, works perfectly. Awesome job, thanks :simple_smile:

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I make an async authenticate calls with passport- ...
I have a working implementation with passport-local-mongoose but would like to use async/await to keep things consistent, avoid nested ...
Read more >
How To Implement API Authentication with JSON Web ...
This guide will walk you through how to implement authentication for an API using JWTs and Passport, an authentication middleware for Node.
Read more >
Everything you need to know about the `passport-jwt` ...
Intro to jsonwebtoken and passport-jwt configuration; What about Angular? How does that handle JWTs? JWT Based Authentication Implementation ...
Read more >
Async Await for Passport.js Authentication in Express.js
The goal of this blog post is for you to know exactly how to implement the LocalStrategy for PassportJS using async/await patterns in...
Read more >
How to implement JWT authentication in NestJS
Now, let's implement a JSON web token to authenticate users into the application. ... Here, we implemented a passport-local strategy to ...
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