title: automated login for rocketchat user within iframe embedded in an angular 6 component
body: <p>I am attempting to login users automatically into my rocketchat server via my angular 6 application/component ( where users are already required to login). </p>
  
  <p>Does anyone have any experience directly manipulating the mongodb to login a user with rocket chat? I can update the database with a generated token, but cannot get iframe to read/accept the generated token.</p>
  
  <p>I have direct access to the mongodb/rocketchat database which I am attempting to manipulate to store a token for login access. I am storing a generated token (I am using a UUID) onto the user document under the fields
  services: { iframe: "generated-token" }
  where generated-token is replaced with my uuid.</p>
  
  <p>I set the same token under window.localstorage and post a message as requested in the documentation here: <a href="https://rocket.chat/docs/developer-guides/iframe-integration/authentication/#managing-mongodb-directly" rel="nofollow noreferrer">https://rocket.chat/docs/developer-guides/iframe-integration/authentication/#managing-mongodb-directly</a></p>
  
  <p>For the life of me I cannot get this to work. I am not getting any errors in the console - so it doesnt appear to be a CORS issue. </p>
  
  <p>The rocketchat server whilst in development is hosted on a separate machine on my network (<a href="http://project-mgr:3000" rel="nofollow noreferrer">http://project-mgr:3000</a>).</p>
  
  <p>I am using my local machine for development (<a href="http://localhost:4000" rel="nofollow noreferrer">http://localhost:4000</a>).</p>
  
  <pre><code>//component
  login() {
        //retrieve a list of rocketchat users from mdb
        this.rocketService.allUsers().subscribe((au) =&gt; {
            //retrieve current user from mdb
            let user = au[this.user.mail]; // 
  
            //generate token
            let token = API.UUID();
  
            //manipulate rocketchat mdb entry with token info
            user.services = { iframe: token }
  
            //update mdb entry for user
            this.rocketService.login(user).subscribe(() =&gt; {
  
              //set local storage with token info
              window.localStorage.setItem("Meteor.userId", user._id);
              window.localStorage.setItem("Meteor.loginToken", token);
  
              //post message
              window.parent.postMessage({
              event: 'try-iframe-login'
            }, 'http://localhost:4000');
          });
        })
    }
  
  ngOnInit() {
      window.addEventListener('message', (event:any) =&gt;
            console.log(event)
       );
  
      this.subscriptions.push(
        //retrieve currently logged in user
        this.authService.user.subscribe((u) =&gt; {
          this.user = new User(u);
  
          //login to rocket chat
          this.login();
          }))
    }
  
  //html - not safe, but for development only
  
  &lt;div fxLayout="column" fxLayoutAlign="stretch"
  style="height:calc(100vh - 80px);width:100%;"&gt;
  
    &lt;iframe #rocketChat frameborder="0" width="100%" height="100%"
    sandbox="allow-same-origin allow-scripts allow-forms allow-top-navigation allow-popups
            allow-popups-to-escape-sandbox" [src]="'http://project-mgr:3000' | safe: 'resourceUrl'"&gt;
    &lt;/iframe&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>The login interface for rocketchat does not change ( or login ) but there are two messages generated from the event subscription.</p>
  
  <p>They have event data:</p>
  
  <p>data: ""
  eventName: "unread-changed"</p>
  
  <p>and </p>
  
  <p>data: true
  eventName: "startup"</p>
  
  <p>I have not been able to figure out if that is useful or irrelevant.</p>