Linking Twitter account to user account (twitter-passport)

huangapple go评论81阅读模式
英文:

Linking Twitter account to user account (twitter-passport)

问题

Currently, a user is able to login in and sign up for my application no problem. I've then added a "Link your twitter user to account" button which when clicked takes the user to '/auth/twitter'. This then kicks off passport-twitter and the oAuth process begins.

Right now, I'm using passport-twitter as the package for twitter oAuth. This process works. I'm able to get the user successfully authenticated. Here is the code.

However two problems: I don't see a way to 1) keep the user signed into Twitter so they don't have to keep doing this flow of reconnecting their twitter every time they want to push content to it from my app. and 2) associate the Twitter user and the signed in user to my application. Long term, I plan to add other social media accounts, so the user will have multiple social media linked. Twitter will be just one.

Problem #2: I wasn't able to do an axios.get call from my redux store or from the front end to '/auth/twitter/' otherwise I could then just get the information back from the call and then post it to the user's table (right?). So, instead I'm accessing '/auth/twitter' from an tag in the front end to kick off the flow.

The user is being redirected to /AccountSettings while they go through this flow, so I know that the user is 100% authenticated and signed in with Twitter (otherwise they'd be pushed to /login, which isn't happen).

Most people in this flow create a user in their database using the information returned from Twitter.

However, I'm trying to link this information to the signed in user, and keep them signed into Twitter so the user doesn't need to keep reconnecting their Twitter account (at least not often). (With access to their Twitter account, my plan is to allow them to push content to it)

Currently I'm hitting the '/auth/twitter' route with an tag which's href takes it to '/auth/twitter'. Is this the right way about it or is this approach causing my linkage issue?

What are people's recommendation for this issue? Whats the right way to approach linking social media accounts to a signed in user's account?

I'm using Express, Redux, React, Postgres, and passport-twitter

英文:

Currently, a user is able to login in and sign up for my application no problem. I've then added a "Link your twitter user to account" button which when clicked takes the user to '/auth/twitter'. This then kicks off passport-twitter and the oAuth process begins.

Right now, I'm using passport-twitter as the package for twitter oAuth. This process works. I'm able to get the user successfully authenticated. Here is the code.

However two problems: I don't see a way to 1) keep the user signed into Twitter so they don't have to keep doing this flow of reconnecting their twitter every time they want to push content to it from my app. and 2) associate the Twitter user and the signed in user to my application. Long term, I plan to add other social media accounts, so the user will have multiple social media linked. Twitter will be just one.

Problem #2: I wasn't able to do an axios.get call from my redux store or from the front end to '/auth/twitter/' otherwise I could then just get the information back from the call and then post it to the user's table (right?). So, instead I'm accessing '/auth/twitter' from an <a> tag in the front end to kick off the flow.

passport.use(
  new TwitterStrategy(
    {
      consumerKey: &quot;XXX&quot;,
      consumerSecret: &quot;XXX&quot;,
      callbackURL: &quot;http://localhost:8080/auth/twitter/callback&quot;,
      // callbackURL: &quot;http://www.localhost:8080/home&quot;,
      includeEmail: true,
    },
    async(accessToken, refreshToken, profile, cb) =&gt; {
      console.log(&#39;got the prodile&#39;)
      const twitterIDforOAuth = profile.id
      const { id, username } = profile;


      let theuser = await User.findOne({
        where: {
          twitterID: id
        }
      })


      if(theuser){
        console.log(&#39;FOUND USER&#39;, &#39;\n&#39;, theuser)
      } else {
        try {
          console.log(&#39;NO USER FOUND&#39;)
          var passwordUser = (Math.random() + 1).toString(36).substring(7);
          console.log(&#39;CREATING USER&#39;)
           theuser = await Promise.all([
            User.create({
              twitterID: id,
              username : username,
              password: passwordUser
            })
          ])
          console.log(&#39;USER CREATED&#39;);
        } catch (error) {
          console.log(error);
        }
      }
      //this callback calls the auth/callback url to kick off the redirect process
      // need to send username and password to /auth/signup
      return cb(null, {username: username, password: passwordUser})
      
      //Line below sends too much data that is irrelevant for the user... lets review it?
      // return cb(null, {username: twitterIDforOAuth})
    }
  )
);
app.get(&#39;/auth/twitter&#39;, passport.authenticate(&quot;twitter&quot;));

app.get(
  &quot;/auth/twitter/callback&quot;,
    passport.authenticate(&quot;twitter&quot;, {
      failureRedirect: &quot;/login&quot;,
      failureMessage: true,
      session: false
    }),
   async (req, res) =&gt; {

    var user = req.user;
    console.log(user.username, user.password);
    //GET USERNAME AND PASSWORD
    var username = user.username;
    var password = user.password;
    ///they need to login the app 
    //auth/login

    res.redirect(&#39;/AccountSettings&#39;)
  }
);

The user is being redirected to /AccountSettings while they go through this flow, so I know that the user is 100% authenticated and signed in with Twitter (otherwise they'd be pushed to /login, which isn't happen).

Most people in this flow create a user in their database using the information returned from Twitter.

However, I'm trying to link this information to the signed in user, and keep them signed into Twitter so the user doesn't need to keep reconnecting their Twitter account (at least not often). (With access to their Twitter account, my plan is to allow them to push content to it)

Currently I'm hitting the '/auth/twitter' route with an <a> tag which's href takes it to '/auth/twitter'. Is this the right way about it or is this approach causing my linkage issue?

What are people's recommendation for this issue? Whats the right way to approach linking social media accounts to a signed in user's account?

I'm using Express, Redux, React, Postgres, and passport-twitter

答案1

得分: 0

解决方案:https://stackoverflow.com/questions/18373856/how-to-passing-data-in-twitterstrategy-passportjs

不必翻译的代码部分已被省略。

英文:

SOLUTION: https://stackoverflow.com/questions/18373856/how-to-passing-data-in-twitterstrategy-passportjs

had to create a state object outside the /auth/twitter route and then added a id param to the /auth/twitter route so the full route was /auth/twitter/:id

once I got the id I saved it to a state route outside the route in the server file that was accessible to the callback function later in the proces.

huangapple
  • 本文由 发表于 2023年1月6日 12:00:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75026810.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定