passport icon indicating copy to clipboard operation
passport copied to clipboard

Twitter Strategy not passing query param to req (req.query)

Open boulepick opened this issue 6 years ago • 5 comments

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

boulepick avatar May 08 '18 17:05 boulepick

Same problem here, did you find a solution since your initial post @boulepick ?

antsteyer avatar Jul 24 '19 10:07 antsteyer

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);
  });

boulepick avatar Oct 17 '19 16:10 boulepick

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'}));

selvan avatar Aug 01 '20 17:08 selvan

Saved my life !!

Ankit4you avatar Jul 19 '21 09:07 Ankit4you

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
}

sethmichaelking avatar Feb 13 '23 22:02 sethmichaelking