Skip to content
Prev Previous commit
Next Next commit
Add support for twitter. Close oauth window if the user cancels the a…
…uthentication.

Remove android show setInterval hack because it breaks the behavior of the InAppBrowser closing when the user presses back.
Since loadstop is called without it it is not necessary.
  • Loading branch information
jperl committed Jun 10, 2014
commit acae34c50e6add9bc0629e81ae1bdf517cad86fd
43 changes: 19 additions & 24 deletions patch_window.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ window.patchWindow = function () {

// Keep a reference to the in app browser's window.open.
var __open = window.open,
oauthWin,
timer;
oauthWin;

// Create an object to return from a monkeypatched window.open call. Handles
// open/closed state of popup to keep Meteor happy. Allows one to save window
Expand All @@ -30,33 +29,31 @@ window.patchWindow = function () {

oauthWin.addEventListener('loadstop', checkIfOauthIsDone);

// use hidden=yes as a hack for Android, allows popup to yield events with
// each #show call. Lets events run on Meteor app, otherwise all listeners
// will *only* run when tapping done button or oauthWin.close
//
// XXX should be a better way to do this
if (device.platform === 'Android') {
timer = setInterval(oauthWin.show, 200);
} else {
oauthWin.show();
}
oauthWin.show();

// check if uri contains an error or code param, then manually close popup
function checkIfOauthIsDone(event) {
// XXX improve this regex to match twitters as well
if (!event.url || !event.url.match(/error|code=/)) return;
// if this is the oauth prompt url, we are not done
if (url === event.url) return;

if (!event.url || !event.url.match(/close|error|code=/)) return;

if (event.url.indexOf('credentialToken') > -1) {
// Get the credentialToken and credentialSecret from the InAppBrowser's url hash.
var hashes = event.url.slice(event.url.indexOf('#') + 1).split('&');
var credentialToken = hashes[0].split('=')[1];

// Get the credentialToken and credentialSecret from the InAppBrowser's url hash.
var hashes = event.url.slice(event.url.indexOf('#') + 1).split('&');
var credentialToken = hashes[0].split('=')[1];
var credentialSecret = hashes[1].split('=')[1];
if (event.url.indexOf('credentialSecret') > -1) {
var credentialSecret = hashes[1].split('=')[1];
OAuth._handleCredentialSecret(credentialToken, credentialSecret);
}

Accounts.oauth.tryLoginAfterPopupClosed(credentialToken);
}

OAuth._handleCredentialSecret(credentialToken, credentialSecret);
Accounts.oauth.tryLoginAfterPopupClosed(credentialToken);
oauthWin.close();

clearInterval(timer);
oauthWin.removeEventListener('loadstop', checkIfOauthIsDone)
oauthWin.removeEventListener('loadstop', checkIfOauthIsDone);
}

this.closed = false;
Expand All @@ -65,8 +62,6 @@ window.patchWindow = function () {
close: function () {
if (!oauthWin) return;

clearInterval(timer);

oauthWin.close();
this.closed = true;
}
Expand Down