Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Twitter Authenticate with multiple hostnames from one domain using the same Twitter App?

I have two hostnames that get sent to the same IP by my DNS:

  • theark.info
  • www.theark.info

I explicitly set my callback and domain name in my Twitter App using theark.info. What is the best way to make sure I can use the same Twitter App for Oauth using www.theark.info, as I currently get an error:

Internal Server Error

In my DNS I have a CNAME www in my DNS that points to theark.info

Maybe I need to manipulate the DOM using Express and Javacsript on requests?

like image 795
EhevuTov Avatar asked Nov 04 '22 18:11

EhevuTov


1 Answers

You can't change Twitter (or any other OAuth provider), they all offer only one callback to one domain. An easy solution is to re-route all requests from http://domain.com to http://www.domain.com, so all visitors end up at www.domain.com before authenticating. You should be able to do this on your DNS or with a req.header redirect server-side:

app.get('/*', function(req, res, next) {
  if (req.headers.host.match(/^www/) !== null ) {
    res.redirect('http://' + req.headers.host.replace(/^www\./, '') + req.url);
  } else {
    next();     
  }
})

Copied from this answer.

When authenticating with passport.js, try to specify the callback url:

passport.use(new TwitterStrategy({
    consumerKey: auth_keys.twitter.consumerKey,
    consumerSecret: auth_keys.twitter.consumerSecret,
    callbackURL: auth_keys.twitter.callbackURL
  },
  function(token, tokenSecret, profile, done) {
    process.nextTick(function () {
      User.twitterAuth({ profile: profile }, function (err, user) {
        return done(err, user);
      });
    });
  }
));

And make sure the callbackURL is exactly the same as configured in Twitter. If you're running node for development on localhost, try two different key files and create another authentication apps on twitter with 127.0.0.1:3000 as callback address. You can switch key files for dev and production:

if (app.get('env') == 'development') {
  auth_keys = require('./lib/keys_dev');
} 
if (app.get('env') == 'production') {
  auth_keys = require('./lib/keys_live');
}
like image 85
Patrick Avatar answered Nov 08 '22 07:11

Patrick