I have uploaded an Angular application onto my main tenant using Cumulocity’s Web SDK, version 1019.
The web application needs to make calls to a REST services application hosted on the same tenant. After the user authenticates themselves the application loads, however, a second sign-in popup appears asking the user to re-authenticate themselves again.
I believe this is because the Angular and REST services apps are accessed using separate pathways?
Regardless, I would like the user to bypass this second round of authentication and tried using an OAI cookie token from the c8y/client package as I do not want to have to store my Cumulocity credentials in my source code to retrieve user data.
I created the following service to inject into my application’s core component.
import { Injectable } from '@angular/core';
import { CookieAuth, Client } from '@c8y/client';
@Injectable({
providedIn: 'root'
})
export class AuthenticationService {
private tenantUrl: string = '/api';
private client: Client;
public constructor() {
console.log('AuthenticationService constructor called');
this.client = new Client(new CookieAuth(), this.tenantUrl);
console.log('Client initialized:', this.client);
}
public async getUserInfo() {
console.log('getUserInfo called');
try {
const responsePromise = await this.client.user.current();
console.log('Login with cookie successful');
console.log(responsePromise);
return responsePromise;
} catch (error) {
console.log('Login failed: ', error)
return null;
}
}
}
export class CoreComponent {
public constructor(private authService: AuthenticationService) {
console.log('CoreComponent initialized');
this.authService.getUserInfo().then(userInfo => {
console.log('User Info:', userInfo);
}).catch(error => {
console.error('Error fetching user info:', error);
});
}
}
Note: I am storing the domain URL in a proxy.conf.js file in my application’s root folder.
{
"/api": {
"target": "https://[domain].cumulocity.com",
"secure": false,
"changeOrigin": true,
"pathRewrite": {
"^/api": ""
}
}
}
When I deploy the application to my tenant, however, the authentication fails with the following information displayed in the browser console.
How can I resolve this error?