Home > Software engineering >  Cross Window Communication
Cross Window Communication

Time:01-03

I am currently trying to create a login pop up, sort of like google auth.

I have managed to get everything to work but I have one issue.

Here is how my code works.

  1. User clicks a login button
  2. JS code opens pop up to my website with a login page. (The websites can be cross origin so supposedly there can be no communication between the pop up and the opener site.
  3. Send a POST request to my servers to verify the credentials

I can get these first steps working fine. What I can’t figure out are the next steps

  1. Once the credentials are verified, the pop up closes
  2. An embedded JS script somehow sets headers for the entire website with information about the logged in user

How am I supposed to send the POST response back to the script that opened the pop up?

The main problem is that the sites are cross origin, which blocks communication. Thus I am stumped to find a solution.

CodePudding user response:

I found the answer on page 2 of google results.

https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage

window.postMessage API allows you to listen for events on parent window by adding an event listener for “message” and grab associated data.

  • Related