Search code examples
javascriptnode.jstwitter-oauthloopbackjspassport-twitter

Node.js Loopback Framework + Twitter oauth doesn't work on a live server


I'm using Loopback Third Party Login Passport with Node-Twitter library for api calls.

I've created

var client = new Twitter({
  consumer_key: '',
  consumer_secret: '',
  access_token_key: '',
  access_token_secret: ''
});

which is neccessary for node-twitter to work and I specified all those credentials manually for my twitter account because i'm not able to obtain token and tokenSecret from my accountIdentity model.

I've got an 'add twitter account' button which redirects user to Twitter Authorize page and implements oauth and then redirects user back to the page (on a local machine) but on a live server it redirects to /auth/twitter and then nothing.

I make twitter api calls like friends/ids, users/lookup, account/verify_credentials and they work OK on my local machine. I expected them to work on a live server too, but they don't.

  1. at apps.twitter.com my callback URL is set to http://dev.mysite.com/auth/twitter/callback
  2. providers.json

    { "twitter-login": { "provider": "twitter", "authScheme": "oauth", "module": "passport-twitter", "callbackURL": "dev.mysite.com/auth/twitter/callback", "authPath": "dev.mysite.com/auth/twitter", "callbackPath": "dev.mysite.com/auth/twitter/callback", "successRedirect": "dev.mysite.com", "failureRedirect": "dev.mysite.com/login", "consumerKey": "my key is specified here", "consumerSecret": "my key is specified here", "failureFlash": true } }

(i tried leaving it "as is" which means removing dev.mysite.com, and i also tried adding http:// before url)

  1. npm packages are all up-to-date on a server

my question is how to make it work at least for 1 (my) account for now


Solution

  • after ~3days solved the problem: i should have paid attention to the details

    Loopback adds "/api/" in the global config to all API calls. Otherwise, it treats path as a route.

    providers.json :

    { "twitter-login": { "provider": "twitter", "authScheme": "oauth", "module": "passport-twitter", "callbackURL": "/api/auth/twitter/callback", "authPath": "/api/auth/twitter", "callbackPath": "/api/auth/twitter/callback", "successRedirect": "/", "failureRedirect": "/login", "consumerKey": "key", "consumerSecret": "key", "failureFlash": true } }

    while on the client side I had to add "/api/" to each API call url for example

    function fetchFollowers() {
      const URL = "/api/fetchfollowers";
      return fetch(URL, {method: "GET"
     })
        .then(response=>  Promise.all([response, response.json()]));
    }
    

    and

    <a href="/api/auth/twitter">Add Twitter Account</a>
    

    instead of

    <a href="/auth/twitter">Add Twitter Account</a>