5

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?

laggingreflex
  • 32,948
  • 35
  • 141
  • 196
EhevuTov
  • 20,205
  • 16
  • 66
  • 71
  • Show some code, preferably a minimal test case. – ebohlman Aug 18 '12 at 19:14
  • It's not a code issue. It's twitters auth mechanism, which only allows a callback to one hostname, IIRC. You shouldn't need to see any code. – EhevuTov Aug 18 '12 at 19:19
  • Even if it's a problem with the auth mechanism, some more detail on the Internal Server Error, like a console output could provide better answers. – Patrick Aug 23 '12 at 07:25
  • @Patrick that was the one and only error I received – EhevuTov Aug 23 '12 at 12:58
  • There should be a stacktrace to every error in the node console. If not, you can change your logger: app.use(express.logger('dev')); and error handling: app.use(function(err, req, res, next) { console.error(err.stack); res.send(err); }); – Patrick Aug 25 '12 at 09:43
  • Thank you but I believe this was a twitter error from there site. Express didn't give any error IIRC because this was from twitter.com during the Oauth handshake. – EhevuTov Aug 25 '12 at 16:14

1 Answers1

2

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');
}
Community
  • 1
  • 1
Patrick
  • 7,903
  • 11
  • 52
  • 87
  • How do you rereoute? To my knowledge, DNS just resolves to an IP. It doesn't actually change the domain name your browser talks to or as. I have a CNAME 'www' in my DNS that points to 'theark.info' – EhevuTov Aug 24 '12 at 05:43
  • I edited the answer small pinch to answer the crux of the matter. Basically, I'm pretty sure it will require a server-side HTTP redirect. Thanks for the response. – EhevuTov Aug 24 '12 at 21:06
  • 1
    CNAME just resolves it to the same IP. It doesn't actually change the hostname. I'm going to try a server-side 302 redirect this weekend. This will tell a web client explicitly to change the hostname. – EhevuTov Aug 25 '12 at 16:05