1

For Example:

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
})();

Consider that I have login module in an angular 6 application. How to use this above code and authenticate the user in the login.component.ts file?

Jan Hommes
  • 5,122
  • 4
  • 33
  • 45
PCA
  • 1,677
  • 6
  • 28
  • 45

1 Answers1

1

Cumulocity has released a demo on Stackblitz how to log in the user. Basically you build a ngForm with username, password and tenant and pass that to the Cumulocity client:

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;
    }
}

In this case this.model is the data coming from an ngFrom and on button click the login()? function is executed. The this.cumulocity variable contains a service so that you can share the logged in client with other components.

Note: If you run this on a different server (not hosted), then you need to enable CORS in the Cumulocity administration.

Jan Hommes
  • 5,122
  • 4
  • 33
  • 45
  • Thank you Jan Hommes. One more thing in another example, i saw there were token used to authenticate other services, why the token is not used here. or i'm missing somthing here ? Could you please clarify? – PCA Aug 28 '18 at 12:59
  • @PCA can you point me to that example? We have the possibility to also use a bearer token, but @c8y/client does not support that at the moment. – Jan Hommes Aug 29 '18 at 09:58