views:

1192

answers:

2

Hello all,

I have just succeeded in creating oAuth authentication for my twitter application using PHP.

I then saw this site and I am surprised how they open a new window, close that window and then continue the request in the initial window?! Can Someone explain with some Javascript (I am guessing they are using this) how they did this?!

I notice when the second window closes they make two GET requests.

I want to be able to do something like this since my users can write content on my site and I do not want that to get deleted. Is there a better way that isn't so obtrusive? (window popping open). If not, I will use their method as I can not think of anything else.

Thanks all

+1  A: 

Here's the part of the JavaScript code that is related to that:

TG.util.oauth = {
    win: null,
    timer: null,
    loginUpdate: function() {
        $.getJSON('/-login/check?format=json', TG.util.oauth.loginCallback);
    },
    loginCallback: function(data) {
        if (data && data.loggedin) {
            TG.util.login.update(data);
        }
    },
    winCheck: function() {
        if (!TG.util.oauth.win || TG.util.oauth.win.closed) {
            window.clearInterval(TG.util.oauth.timer);
            return TG.util.oauth.loginUpdate();
        }
    },
    loginClick: function() {
        TG.util.oauth.win = window.open('/-oauth-twitter/request?gotoafter=1&gotor=oauthtwitter&gotop=action%3Dwindowend', 
            'OAuthTwitterRequest', 
            'menubar=yes,location=yes,resizable=yes,scrollbars=yes,status=yes,width=800,height=400');
        if (!TG.util.oauth.win) return true;

        TG.util.oauth.timer = window.setInterval(TG.util.oauth.winCheck, 300);
        return false;
    }
};

TG.util.oauth.win = window.open('/-oauth-twitter/request?gotoafter=1&gotor=oauthtwitter&gotop=action%3Dwindowend','OAuthTwitterRequest','menubar=yes,location=yes,resizable=yes,scrollbars=yes,status=yes,width=800,height=400');
opens the oAuth window, that handles the login

if (!TG.util.oauth.win) return true;
returns true if the window isn't open (I guess).

TG.util.oauth.timer = window.setInterval(TG.util.oauth.winCheck, 300);
spawns a timer that checks if the login has been done every 300 miliseconds.

Pedro Cunha
A: 

Check this: http://dev.twitter.com/anywhere/begin

Jeffrey Gilbert