1

I use angular-auth-oidc-client to implement authentication for a web app using Angular 8, It's working fine when storing user data in the localStorage or sessionStorage but when using a cookies storage implementation, Errors like client:172 [WDS] Disconnected! start shown in the console, and application stop working tell i delete the stored cookies manually.

Console error logs

As the documentation on how to add custom storage here Custom Storage,

The cookie storage implementation i did is:

    import { Injectable, Inject } from '@angular/core';
    import { OidcSecurityStorage } from 'angular-auth-oidc-client';
    import { CookieService } from 'ngx-cookie-service';
    import { OidcConfiguration } from '../config/oidc.config';

    @Injectable({
      providedIn: 'root'
    })
    export class CookiesStorageService implements OidcSecurityStorage {

      constructor(private cookieService: CookieService, @Inject('OidcConfig') private oidcConfig: OidcConfiguration) {
      }

      public read(key: string): any {

          let val = this.cookieService.get(key + '_' + this.oidcConfig.openIdConfiguration.client_id);
          if (val && val != '') {
            return JSON.parse(val);
          }

        return;
      }

      public write(key: string, value: any): void {
        if (value && value != '') {
          value = value === undefined ? null : value;
           this.cookieService.set(
            key + '_' + this.oidcConfig.openIdConfiguration.client_id,
            JSON.stringify(value));
        }
      }
    }

Value for this.oidcConfig.openIdConfiguration.client_id is "OPApiClient" which to append the clientId as a key name postfix.

In the AppModule i register this custom storage as shown:

import { NgModule, Inject } from '@angular/core';
import { CommonModule } from '@angular/common';
import { AuthModule, OidcSecurityService } from 'angular-auth-oidc-client';
...

    @NgModule({
      declarations: [...],
      imports: [
        CommonModule,
        AuthModule.forRoot({ storage: CookiesStorageService }),
        ...
      ],
       providers: [...]
    })

Is there any mistake in the cookies storage that i have or anything i missed? any help would be very thankful.

Bassem Mamar
  • 328
  • 2
  • 14

0 Answers0