Listen to the event in a new window

It is used in my application oauthto allow the user to log in to Salesforce, and after logging in they can access the application. What is happening at the moment:

  • The user clicks the login link and is redirected to Salesforce
  • The user registers with Salesforce and is redirected to the URL I specified
  • My server processes the request and redirects it to the home page

What I would like to do is:

  • The user clicks on the "Login" link and a new window ( window.open) appears on the Salesforce login page.
  • The user logs in and is redirected to the URL I specified
  • When the server is redirected to the main page, the home page fires an event successor logged_inin a window that the original page listens to and interprets

This is what I have done so far (suppose there is <button id="login">Log in</button>)

$('button#login').on('click', function() {
    var popup = window.open('/auth/salesforce', 'login', '...');
    popup.addEventListener('success', function() {
        popup.close();
        alert('Logged in');
    });
});

and on the home page I added to the section that appears when the user successfully logged in:

var event = window.createEvent('loginSuccess');
event.initEvent('success', true, true); 
window.dispatchEvent(event); 

However, the event successnever fires. How can I trigger an event successon the home page to alert the original page on which the user has successfully registered?

Edit: I noticed that there is a method window.postMessageas indicated in the MDN docs . Is that what I should use? Or should I use another method to capture a successful entry event in a newly created one window?

+4
2

, , , IE, - window.opener, , , window.opener. ,

if (window.opener) {
    window.opener.userIsLoggedIn();
}
window.close();

userIsLoggedIn() , .

: IE window.opener - , window.opener null userIsLoggedIn() . close , userIsLoggedIn() , . , window.opener.userIsLoggedIn() , ; . . , , . /.

IE, , window.onclose IE, window.onclose .

0

OAuth . , , , , , ... , ( , salesforce ) - javascript.

0

All Articles