RocketChat / rocketchat_nextcloud

App that allows Rocket Chat to live inside NextCloud and become seamless for the NextCloud Users
28 stars 2 forks source link

When doing local testing, the chat.js sends postMessage before the iframed rocket.chat application is ready to handle it #29

Open mrjsawdk opened 2 years ago

mrjsawdk commented 2 years ago

I am currently testing this integration between NC and rocket.chat.

Following the instructions (and after setting the env. variable CREATE_TOKENS_FOR_USERS=true in Rocket.chat) I am able to access the "rocket chat icon in NC".

Clicking on it has the browser end up on the login screen of Rocket.Chat.

When I look in Rocket.chat and in NC db (oc_rocket_users table), I can see that the integration has actually created the user correctly in both places.

There are no errors in the console but I can see that the token is actually set in the DOM and the postMessage for login-with-token is called.

If I change the chat.js to postpone the logout and login-with-token messages until 2 seconds after the "load" event is handled, then it works, and the user is signed in to rocket chat.

It is of course not a solution to the problem, but more of an attempt to see if it was a timing issue as suspected.

My Setup I run applications in docker.

My updated chat.js

document.addEventListener("DOMContentLoaded", function () {
    var elements = document.getElementsByClassName('messenger--add-members-info');
    if (elements.length > 0) {
        setTimeout(function () {
            elements[0].classList.add('messenger--hidden');
        }, 7000);
    }

    // Always logout the user first, as it could be using a session of another user
    var token = document.querySelector('input[name=rocketchat_token]').value;
    var iframeDoc = document.querySelector('iframe');

    document.querySelector('iframe').addEventListener("load", function() {
    console.log("Scheduling login after load handler");

    setTimeout(function(){
        console.log("Logging out from combined handler");
        iframeDoc.contentWindow.postMessage({
                externalCommand: 'logout',
                token: token
            }, '*');    

        if(token) {
            console.log("Logging in from the combined handler", token);     
            iframeDoc.contentWindow.postMessage({
                externalCommand: 'login-with-token',
                token: token
            }, '*');        
        } else {
            console.log("Could bot find the token");
        }
    },2000);

    });

});

My question Is this just something that happens locally (I have not had this running in a real environment yet), or does the Rocket.chat --> nextcloud integration need a better "ready" sign before the logout and login-with-token messages are sent to the iframe?