-
Hello, do yo have any experience with strategies as ES6 classes? I wrote a Telegram strategy in Typescript. The problem is that the const tgStrategy = new TelegramWidgetStrategy({ botToken }, (_, user, done) => {
log.info(user);
return done(null, user, 'Authentication good');
});
// tgStrategy.name === 'telegram-widget'
passport.use(tgStrategy);
app.use(passport.initialize());
app.get(
'/tg-auth',
// passport.authenticate('telegram-widget') - TypeError: this.success is not a function
// full stack trace, below
passport.authenticate(tgStrategy), // this works
(req, res) => {
res.json(req.user);
}
); The problem originates from: passport/lib/middleware/authenticate.js Lines 188 to 196 in c872f74 I've tried to compile to My use case doesn't allow me to simply use the working approach, as it itself is a plugin to a forum which uses the name approach. What I'm really asking is: Where do I spend my time; making this compatible and PR here or just rewrite the strategy in es5 prototypal? TypeError: this.success is not a function
at TelegramWidgetStrategy.verified (/home/rbeer/code/passport-telegram-widget/dist/lib/strategy.js:20:18)
at TelegramWidgetStrategy.verify (/home/rbeer/code/passport-telegram-widget-demo/index.js:46:10)
at TelegramWidgetStrategy.authenticate (/home/rbeer/code/passport-telegram-widget/dist/lib/strategy.js:52:14)
at attempt (/home/rbeer/code/passport-telegram-widget-demo/node_modules/passport/lib/middleware/authenticate.js:369:16)
at authenticate (/home/rbeer/code/passport-telegram-widget-demo/node_modules/passport/lib/middleware/authenticate.js:370:7)
at Layer.handle [as handle_request] (/home/rbeer/code/passport-telegram-widget-demo/node_modules/express/lib/router/layer.js:95:5)
at next (/home/rbeer/code/passport-telegram-widget-demo/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/rbeer/code/passport-telegram-widget-demo/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/rbeer/code/passport-telegram-widget-demo/node_modules/express/lib/router/layer.js:95:5)
at /home/rbeer/code/passport-telegram-widget-demo/node_modules/express/lib/router/index.js:281:22 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Alright, got it; and it was just me not seeing the wood for the trees. Two things:
Don't do this verified: VerifiedCallback = (err, user, info) => {} Do that verified(err, user, info) {}
this.verify(req, user, this.verified.bind(this)); Otherwise the The wild days of ES5. I guess Crockford was right, after all. 😄 |
Beta Was this translation helpful? Give feedback.
Alright, got it; and it was just me not seeing the wood for the trees.
Two things:
Arrow functions don't have a reference on
this
- ever.Don't do this
Do that
Otherwise the
this
reference inside the callback will beundefined
- or worse something entirely else.The wild days of ES5. I guess Crockford was right, after all. 😄