Making use of Twitter in an app is a simple and straightforward task thanks to great libraries like this one that handle all of the OAuth handshaking and heavy lifting for you. The only problem with the default method is that when a user clicks the connect via Twitter button, it takes them to the Twitter login page and eventually redirects them back to you.
Folks around the Internet are not keen on this approach, preferring a Facebook style integration where a simple pop-up is used to contain the Twitter login process, ensuring the user never loses your page along the way.
First off, grab the Twitter API library if you haven’t already and familiarize yourself with the fantastically simple process. I’m going to modify the included example files to the pop-up feature. First, we add some javascript on the connect.php page (the one with the Twitter button). Add a click event to that connect link that opens a simple window:
The width and height are setup to properly accommodate the Twitter login page. Notice that I am setting the source of the new window to redirect.php, which sets everything up to begin the auth process before sending the pop-up off to Twitter’s login.
The next step is to handle the post-login phase when the user is returned to us. The example uses callback.php as the target for the post-auth redirect, so we’ll modify there. All we have to do there is simply add some javascript to refresh or parent page and close the pop-up and we’re all set. So remove the existing index redirect in the callback file and replace it with some simple javascript and a blank html page:
self.close();
There you have it. The process is essentially the same as the before, only now we’ve moved it all into a separate window that we tightly control.