angular-auth-oidc-client icon indicating copy to clipboard operation
angular-auth-oidc-client copied to clipboard

[Question]: Can someone give an example on how to get config from Http with a standalone configuration

Open guidoffm opened this issue 1 year ago • 4 comments

The current documentation elaborates how to load the config from Http and it points out how to use it in a standalone config. But I am looking for an example that uses both things at once.

guidoffm avatar Apr 26 '24 17:04 guidoffm

After doing some research I found this code piece works, at least for login:

provideAuth({
      loader: {
        provide: StsConfigLoader,
        useFactory: httpLoaderFactory,
        deps: [HttpClient],
      }
    }),

But I found some issues:

  1. The logoff method does not work. It does not throw an error but nothing happens. For static configuration everything is okay.
  2. If I use the httpLoaderFactory from the documentation page then every call of oidcSecurityService.isAuthenticated() e.g. in the html code causes a new http request to get the config.

To work around the 2nd issue I created my own class for that that includes caching:

import { OpenIdConfiguration, StsConfigLoader } from 'angular-auth-oidc-client';
import { HttpClient } from '@angular/common/http';
import { Observable, Subject, map } from 'rxjs';
import { inject } from '@angular/core';


export class MyStsConfigLoader extends StsConfigLoader {

  private configs = new Subject<OpenIdConfiguration[]>;
  private readonly httpClient = inject(HttpClient);
  /**
   *
   */
  constructor() {
    super();
    console.log('MyStsConfigLoader.constructor');
    this.httpClient.get<{
      clientId: string;
      scope: string;
      authority: string;
    }>(`config/auth-config.json`).pipe(
      map((customConfig) => {

        console.log('customConfig', customConfig);

        const config: OpenIdConfiguration = {
          // authority: 'https://idsvr4.azurewebsites.net',
          redirectUrl: window.location.origin,
          postLogoutRedirectUri: window.location.origin,
          // clientId: 'spa',
          // scope: 'openid profile offline_access', // 'openid profile offline_access ' + your scopes
          responseType: 'code',
          silentRenew: true,
          useRefreshToken: true,
          renewTimeBeforeTokenExpiresInSeconds: 30,
        };

        config.authority = customConfig.authority;
        config.clientId = customConfig.clientId;
        config.scope = customConfig.scope;
        console.log('config', config);
        return [config];
        
      })).subscribe(x => this.configs.next(x));
  }

  override loadConfigs(): Observable<OpenIdConfiguration[]> {
    return this.configs;
  }

}

guidoffm avatar Apr 27 '24 13:04 guidoffm

The logoff method not working for me. Have you find a solution ?

krabouilleur avatar May 21 '24 09:05 krabouilleur

The logoff method not working for me. Have you find a solution ?

No, I haven't.

guidoffm avatar May 21 '24 12:05 guidoffm

The logoff method not working for me. Have you find a solution ?

No, I haven't.

Is there an alternative ?

krabouilleur avatar May 22 '24 16:05 krabouilleur