passport
passport copied to clipboard
Twitter Strategy not passing query param to req (req.query)
Hello All,
there seem to be an issue with the "twitter strategy" not passing additional data in the authenticate function. Belo i have included both the facebook and twitter strategy, where FB is working fine but not twitter.
Facebook route and strategy
router.get('/reg_facebook/:action/:member', (req, res) => {
console.log(req.params); // { action: 'test', member: 'user' }
passport.authenticate(
'facebook',
{
scope: socAuth.facebookAuth.scope,
state: JSON.stringify(req.params),
}
)(req, res);
});
passport.use(new FacebookStrategy(
{
clientID: socAuth.facebookAuth.clientID,
clientSecret: socAuth.facebookAuth.clientSecret,
callbackURL: socAuth.facebookAuth.callbackURL,
profileFields: ['id', 'email', 'first_name', 'last_name'],
passReqToCallback: true,
},
(async (req, accessToken, refreshToken, profile, next) => {
try {
console.log(req.query); //state: '{"action":"test","member":"user"}' }
const addedParam = JSON.parse(req.query.state); /
.....
}))
and the twitter route and strategy is as follow:
router.get('/reg_twitter/:action/:member', (req, res) => {
console.log(req.params); //// { action: 'test', member: 'user' }
passport.authenticate(
'twitter',
{
state: JSON.stringify(req.params),
}
)(req, res);
});
passport.use(new TwitterStrategy(
{
consumerKey: socAuth.twitterAuth.consumerKey,
consumerSecret: socAuth.twitterAuth.consumerSecret,
callbackURL: socAuth.twitterAuth.callbackURL,
passReqToCallback: true,
},
(async (req, token, tokenSecret, profile, next) => {
try {
console.log(req.query); // undefined
const addedParam = JSON.parse(req.query.state);
.....
}))
as you can see i am getting and "undefined" in the console log for the twitter code.
Thank you
Same problem here, did you find a solution since your initial post @boulepick ?
Hello, i bypassed it by using the session and retrieving the values from there ( see the code inside the double asterix). hope that can help
router.get('/reg_twitter/:action/:member', (req, res) => {
**req.session.state = JSON.stringify(req.params);**
passport.authenticate(
'twitter',
{
state: JSON.stringify(req.params),
}
)(req, res);
});
passport-twitter (http://www.passportjs.org/packages/passport-twitter/) uses OAuth 1.0a, attaching query params in the callbackURL solved the problem for me,
app.get('/auth/twitter',
passport.authenticate('twitter', {callbackURL: '/twitter/callback?state=hello'}));
Saved my life !!
but then I don't seem to be able to access the done/cb function I pass in?
app.get('/auth/twitter/:id', (req, res) => {
const id = req.params.id
passport.authenticate("twitter", {
callbackURL: `/auth/twitter/callback?state=${id}`,
successRedirect: '/accountSettings'
})(req, res)
})
passport.use(new TwitterStrategy(
{
consumerKey: "xxx",
consumerSecret: "xxx",
callbackURL: "http://localhost:8080/auth/twitter/callback",
passReqToCallback: true,
includeEmail: true,
},
async(req, accessToken, tokenSecret, profile, cb) => {
// console.log(accessToken, tokenSecret, refreshToken, profile, req.query)
const username = profile.username
const id = req.query.state
/// cb/next is undefined
}