Rocket.Chat icon indicating copy to clipboard operation
Rocket.Chat copied to clipboard

IFrame login

Open fabiancook opened this issue 9 years ago • 9 comments

Hi,

We are logging in using

iframe.contentWindow.postMessage({
        externalCommand: 'login-with-token',
        token: $scope.authToken
      }, '*');

Sometimes we get "You have been logged out" in the console, which we can not automatically detect from outside the iframe, meaning a user has to refresh the page and it logs in them as normal.

Can we please have a way to pick up on this so we can resend the token?

Thanks

Rocket.Chat Version: 0.54.2 Running Instances: 1 DB Replicaset OpLog: Enabled Node Version: 4.6.2 - x64

fabiancook avatar Apr 30 '17 22:04 fabiancook

Same issue here. Our workaround is to catch errors in the custom script and forward it to the external page.

var sourcePage = null;
var sourcePageOrigin = null;

window.onerror = function (err) {
    if (sourcePage) {
        sourcePage.postMessage({eventName: 'global-chat-error'}, sourcePageOrigin);
    }
    return false;
};

window.myHandler = function (e) {
        sourcePage = e.source;
        sourcePageOrigin = e.origin;
}

window.addEventListener('message', window.myHandler);

The problem is that we need to keep a reference to the external page, so this works only if the external page previously posted a message to the iframe. Also, we are catching errors too broadly. It would be much better if the logged-out event was properly handled in the RocketChat client code and a new event raised afterwards.

andreaazzara avatar May 02 '17 11:05 andreaazzara

Hi,

Thanks for the code, it appears the result is just logged however... console.log("Iframe command [login-with-token]: result", arguments)

Looks like I will be monkey patching console log and watching for that result!

fabiancook avatar May 11 '17 02:05 fabiancook

image

fabiancook avatar May 11 '17 02:05 fabiancook

I have now been able to receive updates about the login.

It would be awesome if there was an officially documented way to receive these updates.

(function(window, console) {

  var sourcePageMessages = [];

  window.onerror = function (err) {
    if (window !== window.parent) {
      window.parent.postMessage({ eventName: 'global-chat-error', error: err });
    }
    return false;
  };

  var originalConsoleLog = console.log.bind(console);

  console.log = function() {

    var args = Array.prototype.slice.call(arguments);

    if (args[0] !== 'Iframe command [login-with-token]: result') {
      return originalConsoleLog.apply(console, args);
    }

    if (window !== window.parent) {
      window.parent.postMessage({eventName: 'global-chat-login-results', eventData: args[1][0]}, '*');
    }

    return originalConsoleLog.apply(console, args);
  };

  console.log('Global chat error listener added');

}(window, console));

fabiancook avatar May 11 '17 03:05 fabiancook

This is what we use in the parent window:

      if(!(
        event &&
        event.data &&
        event.data.eventName === 'global-chat-login-results' &&
        event.data.eventData && // If this is null login was successful
        event.data.eventData.reason === "You've been logged out by the server. Please log in again."
        )) {
        return;
      }

      console.warn('Reloading due to logout');
      $window.location.reload();
    });

fabiancook avatar May 11 '17 03:05 fabiancook

+1

alaaellethey avatar Sep 13 '17 13:09 alaaellethey

+1

Alaa-Ellethey avatar Sep 13 '17 13:09 Alaa-Ellethey

+1

bsubasic avatar Oct 10 '17 15:10 bsubasic

We have a page event called Custom_Script_Logged_Out which is fired on logout now.

gabriellsh avatar Sep 19 '23 14:09 gabriellsh