Closed noahjahn closed 4 years ago
Not setting the headerConfig.prefix value results in the middleware function never being called.
headerConfig.prefix
For example, here is where I'm using the strategy:
passport.use(new HeaderAPIKeyStrategy( { header: 'X-Api-Key' }, false, function (apikey, done) { console.log('hello from headerapikeystrategy'); User.findOne({ apikey: apikey }, (err, user) => { if (err) { console.error(err); return done(err); } if (!user) { return done(null, false); } return done(null, user); }); } ));
And using this strategy as express middleware:
router.post('/login', passport.authenticate('headerapikey', { session: false }), (req, res) => { console.log('authenticated!'); res.status(200).sendFormat(req, res); });
The server always responds with 401 Unauthorized and I never see 'hello from headerapikeystrategy' printed out in the console.
To fix this, I had to pass an empty string to the headerConfig.prefix value in the constructor:
passport.use(new HeaderAPIKeyStrategy( { header: 'X-Api-Key', prefix: '' }, false, function (apikey, done) { console.log('hello from headerapikeystrategy'); User.findOne({ apikey: apikey }, (err, user) => { if (err) { console.error(err); return done(err); } if (!user) { return done(null, false); } return done(null, user); }); } ));
Not setting the prefix should default to empty.
Hi @noahjahn Thanks a lot for your issue. Trying to fix this tomorrow.
published as v1.2.2
Not setting the
headerConfig.prefix
value results in the middleware function never being called.For example, here is where I'm using the strategy:
And using this strategy as express middleware:
The server always responds with 401 Unauthorized and I never see 'hello from headerapikeystrategy' printed out in the console.
To fix this, I had to pass an empty string to the
headerConfig.prefix
value in the constructor:Not setting the prefix should default to empty.