'Passport JS OAuth2Strategy does't fire callback
After updating my dependencies at service I'm creating I faced the issue that my Passport JS strategy, used along with OAuth2Strategy stoped working properly.
Here are the prerequisite:
- Node:
14.15.1
- pnpm:
6.29.0
- passport:
0.5.2
- passport-oauth2
1.6.1
- express-session
1.17.2
Strategy is initialised like follows:
const passport = require('passport');
const config = require('../../../../config');
const OAuth2Strategy = require('passport-oauth2');
const axios = require('axios');
const MyStrategy = new OAuth2Strategy({
// state: true,
authorizationURL: config.myapi.authorizationURL,
tokenURL: config.myapi.tokenURL,
clientID: config.myapi.clientId,
clientSecret: config.myapi.clientSecret,
callbackURL: config.myapi.callbackURL,
passReqToCallback: true,
}, () => {console.log('Fire!')}); // <- This line should be called, but it is not!
passport.use('oauth2', MyStrategy);
*Obviously, the part where fire is written should be replaced by callback function, but I replaced it for more cleaner code
And routes go this way
...
routes.get('/oauth/myapi', async (req, res, next) => {
const authParams = {
session: true,
scope: 'read, create',
state: req.csrfToken(),
};
return passport.authenticate('oauth', authParams,
async (err, passportUser, info) => {
if (err) {
return next(err);
}
if (passportUser) {
const user = passportUser;
user.token = passportUser.generateJWT();
return res.json(user.toAuthJSON());
}
res.status(400).json({error: info});
})(req, res, next);
});
routes.get('/oauth/myapi/callback',
async (req, res, next) => {
return passport.authenticate('oauth', {
// failWithError: true,
successRedirect: '/dashboard',
failureRedirect: '/login/oauth/myapi/failed'
})(req, res, next);
});
...
So in a callback I do receive response from third service and it looks like this
{
"code":"2QoCKOzHQbCdJID4m...pwHv4M1RqUKjKF",
"state":"dS9Gagcc-....a_yJ71YU",
"user_id":"101"
}
But when callback route attempts to execute passport.authenticate I receive
$ Error: Failed to obtain access token $ at /Users/number16/Documents/GitHub/Video-Mixer-Node/node_modules/.pnpm/[email protected]/node_modules/passport-oauth2/lib/strategy.js:178:49
Debugging didn't help much either.
It seems to me that I do something wrong or I should update my code as some breaking change might require.
The problem seems to be caused by switching from 0.4.1
to 0.5.2
passport
Please, provide me with suggestions on what might cause this issue and how to resolve it.
Thanks in advance
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|