1

Related question: OAuth (Instagram) without refresh

The Situation

Instagram expects a full redirect for authentication, and then they'll redirect back. I want it to work with a popup.

Possible Solution

Open the page in a popup:

var authWindow = window.open(instagramUrl, 'authWindow');

Then I'll have instagram redirect to a page that closes itself:

<script>window.close()</script>

The Problem

I can't figure out how to find out when the window is closed. The following both don't work:

authWindow.onbeforeunload = function() {
   window.opener.console.log('Closing popup!');
};

function logClose(cnsl) { cnsl.log('Closing popup!'); }
var callback = _.bind(logClose, window, console);
$(authWindow.document).click(callback);

Alternative suggestions are very welcome!

Community
  • 1
  • 1
AJcodez
  • 31,780
  • 20
  • 84
  • 118

1 Answers1

6

Instead of using <script>window.close()</script> directly, you could call a function in the parent window with window.opener and then close the window:

Parent:

function onInstagramAuth()
{
  // Handle however you would like here
  console.log('authenticated');
}

Child:

try { window.opener.onInstagramAuth(); } catch (err) {}
window.close();
AJcodez
  • 31,780
  • 20
  • 84
  • 118
keyneom
  • 815
  • 10
  • 12