Skip to content

Instantly share code, notes, and snippets.

@BenJam
Created December 13, 2010 12:05
Show Gist options
  • Save BenJam/738935 to your computer and use it in GitHub Desktop.
Save BenJam/738935 to your computer and use it in GitHub Desktop.
Ribbit iFrame oAuth hack
Ribbit.getAuthenticatedUserIniFrame = function(callback, name, windowOptions) {
var win = null;
var gotUrlCallback = function(result) {
console.log(result);
if (result.hasError) {
callback(new Ribbit.RibbitException("Cannot get request token, check application credentials.", 0)); //the request for an oAuth uri has gone wrong
} else {
var timeOutPoint = new Date().getTime() + 300000;
var pollApproved = function() {
var callback = function(val) {
//setInterval
if (!val.hasError) {
callback(true);
console.log('We\'re in!');
win.remove(); //remove the iFmame
$('.body').show(); //show the body of the DOM
return;
} else if (new Date().getTime() > timeOutPoint) { // leave timeout in removing iFrame and giving control back
callback(new Ribbit.RibbitException("Timed out.", 0)); //timed out
} else {
pollApproved();
}
};
Ribbit.checkAuthenticatedUser(callback); //returns true if we have an authenticated user (Ribbit.userID != null) for the browser
};
win = $('<iframe src ="'+result+'" width="100%" height="100%"><p>Your browser does not support iframes.</p></iframe>'); //create an iFrame to overlay atop the current page
$('.body').hide(); //hide the body
pollApproved(); //poll the iFrame by merely checking the Ribbit userid
}
};
Ribbit.createUserAuthenticationUrl(gotUrlCallback); //request to Ribbit paltform for one time oAuth URI
};
@san1t1
Copy link

san1t1 commented Dec 13, 2010

  1. $(".body") gets all elements with a class of body, whereas $("body") gets all elements (only one) with a tagName of body.
  2. if you hide the body you have nowhere to put the iframe
  3. while line 26 creates the iframe, it's only a document fragment - you have to $(".selector").append(win) somewhere.

@san1t1
Copy link

san1t1 commented Dec 13, 2010

  1. line 24 - wrap this is in a setTimeout(function(){Ribbit.checkAuthenticatedUser(callback);},1000) - you only need to check about once a second

@san1t1
Copy link

san1t1 commented Dec 13, 2010

think this is last - when you timeout, you need to remove the iframe and restore any hidden elements too.

@san1t1
Copy link

san1t1 commented Dec 13, 2010

oh, a fyi - line 28, the comment is wrong. you're polling to check that the request token has been approved by attempting to exchange it for an access token. not polling userid.

@BenJam
Copy link
Author

BenJam commented Dec 13, 2010

wise words. Thanks tim!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment