angular - 在嵌入 Angular 6 组件的 iframe 中自动登录 rocketchat 用户

标签 angular mongodb iframe rocket.chat

我正在尝试通过我的 angular 6 应用程序/组件(用户已经需要登录)自动将用户登录到我的 rocketchat 服务器。

有没有人有直接操作 mongodb 来使用 rocket chat 登录用户的经验?我可以使用生成的 token 更新数据库,但无法让 iframe 读取/接受生成的 token 。

我可以直接访问 mongodb/rocketchat 数据库,我试图操纵它来存储用于登录访问的 token 。我正在将生成的 token (我正在使用 UUID)存储到字段下的用户文档中 服务:{ iframe:“生成的 token ”} 其中 generated-token 替换为我的 uuid。

我在 window.localstorage 下设置了相同的标记,并按照此处文档中的要求发布消息:https://rocket.chat/docs/developer-guides/iframe-integration/authentication/#managing-mongodb-directly

对于我的生活,我无法让它发挥作用。我在控制台中没有收到任何错误 - 所以它似乎不是 CORS 问题。

开发中的 rocketchat 服务器托管在我网络上的另一台机器上 (http://project-mgr:3000)。

我正在使用本地机器进行开发 ( http://localhost:4000 )。

//component
login() {
      //retrieve a list of rocketchat users from mdb
      this.rocketService.allUsers().subscribe((au) => {
          //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(() => {

            //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) =>
          console.log(event)
     );

    this.subscriptions.push(
      //retrieve currently logged in user
      this.authService.user.subscribe((u) => {
        this.user = new User(u);

        //login to rocket chat
        this.login();
        }))
  }

//html - not safe, but for development only

<div fxLayout="column" fxLayoutAlign="stretch"
style="height:calc(100vh - 80px);width:100%;">

  <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'">
  </iframe>
</div>

rocketchat 的登录界面没有变化(或登录),但事件订阅生成了两条消息。

他们有事件数据:

数据:“” eventName: "未读-更改"

数据:真实 事件名称:“启动”

我无法弄清楚这是有用还是无关紧要。

最佳答案

这样做很简单,Rocket Chat iframe 集成使得向 iframe 发送命令变得容易,其中一个命令是“login-with-token”。

首先,您需要配置 rocket 聊天服务器以接受 iframe 集成:

Administration -> General -> iframe integration

iframe integration

并根据您使用的 web/ui 技术,将以下代码添加到您的 html 页面:

//Read the following values based on the web site development technology you are using
//You might need also to create the group/user if not exist, and then add user to the group

    string authToken = "####"; //after you sign in the user
    string groupName = "GGGG"; //after you create a group (if not)
    string channelLink = "https://your.rchat.url.com/channel/" + groupName + "?layout=embedded";


//now use the channelLink in the iFrame
<iframe id="rcChannel" name="rcChannel" src="@channelLink" width="400" height="700"></iframe>



//And here you can authenticate the IFrame
<script>
    function authenticateIFrame() {
        document.getElementById('rcChannel').contentWindow.postMessage({
            externalCommand: 'login-with-token',
            token: '@authToken'
        }, '*');
    }

    window.onload = function () {
        authenticateIFrame();
    };
</script>

希望这会有所帮助。

关于angular - 在嵌入 Angular 6 组件的 iframe 中自动登录 rocketchat 用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55607948/

相关文章:

angular - @Viewchild 在 ngOnInit 期间未初始化

java - 如何处理socket断开和心跳消息?

javascript - 搜索时禁用输入字段并将焦点设置为完成

r - 使用 R rvest 库在 iframe 中抓取表

Angular 8 ng-build 使用 cordova 抛出 MIME 错误

mongodb - 在调用 Execute 之前,插入操作必须具有部署集

json - 来自 MongoDB 的日期打印为 "date":"2011-05-12T13:51:33Z"

json - OData URI 到 JSON mongoDB 查询

javascript - iframe 全屏 100% 宽度

html - 如何让 iframe 占用所有剩余空间