2

How to promisify function what is Express middleware (for example authenticate(arg1, arg2)(req, res, next))?

I tried promisify method from util and also from es6-promisify but none of those work. I am getting error:

TypeError: authenticate(...) is not a function

when I do:

const authenticate = promisify(passport.authenticate);

(passport.authenticate) is the middleware.

My actual problem is: I am using passport-jwt module for passport and I want to reuse passport.authenticate middleware as normal isLoggedIn function. I do not want to make same function to check JWT when there is this function. Also I do not want to promisify this middleware manually because I will be doing it in many places and its code repetition.

Baterka
  • 3,075
  • 5
  • 31
  • 60
  • You will have to show people what you're actually trying to accomplish (the end goal, not just this step in the process). Middleware is a special type of function. With Express 4, you would not return a promise from middleware - that's not how Express works. It sounds like you're trying to solve some problem the wrong way so we need to see the higher level problem. – jfriend00 Sep 17 '19 at 18:35
  • @jfriend00 thx, I added actual problem – Baterka Sep 17 '19 at 18:41
  • Well, middleware works differently than a plain `isLoggedIn()` function would work. Usually, your middleware would set something in the session and `isLoggedIn()`would check the current user's session to see if they are logged in. Completely different functions that work differently and do different things. You can probably factor out some common code and have each function share some code, but a middleware function is a special type of function you would not use directly in some other context. – jfriend00 Sep 17 '19 at 18:44
  • Ohh, and middleware does not have the right kind of function signature or style of working to promisify it automatically either. You may need to break out a common component, you can promisify and share. – jfriend00 Sep 17 '19 at 18:48

0 Answers0