Node.js passport-twitter: Unable to get user email in passport-twitter authentication

95 Views Asked by At

I want to get the user email when the user is logging in with the Twitter strategy within the profile object. I searched for answers. I found these links where people are saying to use includeEmail or the URL with include_email endpoint. Sadly their problem got solved but mine is not getting fixed with the same strategy. see my code:

strategy code:

//necessary imports

module.exports = (passport) => {
  passport.use(
    new Strategy(
      {
        consumerKey: process.env.TWITTER_CONSUMER_KEY,
        consumerSecret: process.env.TWITTER_CONSUMER_SECRET,
        callbackURL: "/api/auth/twitter/callback",
        includeEmail: true,
      },
      async function (token, tokenSecret, profile, cb) {
        console.log("profile", profile)
        const userEmail = profile.emails[0].value
        const q = query(
          Users,
          where("strategy", "==", "twitter"),
          where("email", "==", userEmail)
        )
        const snap = await getDocs(q)
        if (snap.docs.length !== 0) {
          cb(null, snap.docs[0].data())
        } else {
          // Generate a username
          const username = await usernameGenerator(userEmail)
          // Create a user
          const newUserRef = await addDoc(Users, {
            strategy: "twitter",
            username,
            email: userEmail,
          })
          const newUserSnap = await getDoc(newUserRef)
          return cb(null, newUserSnap.data())
        }
      }
    )
  )

  passport.serializeUser(function (user, done) {
    done(null, user)
  })
  passport.deserializeUser(function (obj, done) {
    done(null, obj)
  })
}

here profile doesn't have any array of emails. enter image description here

0

There are 0 best solutions below