如何在Angular 6 App中使用c8yClient代码(打字稿文件)

时间:2018-08-27 06:34:03

标签: cumulocity

例如:

import { Client } from '@c8y/client';

const baseUrl = 'https://demos.cumulocity.com/';
const tenant = 'demos';
const user = 'user';
const password = 'pw';

(async () => {
  const client = await Client.authenticate({
    tenant,
    user,
    password
  }), baseUrl);
  const { data, paging } = await client.inventory.list();
  // data = first page of inventory
  const nextPage = await paging.next();
  // nextPage.data = second page of inventory
})();

考虑我在angular 6应用程序中具有登录模块。如何使用上面的代码并在login.component.ts文件中对用户进行身份验证?

1 个答案:

答案 0 :(得分:1)

Cumulocity已发布demo on Stackblitz如何登录用户。基本上,您使用用户名,密码和租户构建一个ngForm,并将其传递给Cumulocity客户端:

async login() {
    const client = new Client(new BasicAuth(
      {
        user: this.model.user,
        password: this.model.password,
        tenant: this.model.tenant
      }),
      `https://${this.model.tenant}.cumulocity.com`
    );

    try {
      let user = await client.user.current();
      this.cumulocity.client = client;
    } catch (ex) {
      this.cumulocity.client = null;
      this.error.shown = true;
      this.error.msg = ex.message;
    }
}

在这种情况下,this.model是来自ngFrom的数据,然后单击按钮执行login()?函数。 this.cumulocity变量包含一项服务,以便您可以与其他组件共享已登录的客户端。

  

注意:如果您在其他服务器(未托管)上运行此服务器,则需要在Cumulocity管理中启用CORS。