IFrame login
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
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.
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!

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));
This is what we use in the parent window:
+1
+1
+1
We have a page event called Custom_Script_Logged_Out which is fired on logout now.