Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Service is not included in deployed pages

I’m new to angular, I wrote an interceptor to test Basic Authentication, the problem is that the interceptor is never called. So I tried to launch my application in debug mode and what I discovered that interceptor is not included in deployed pages.
Is it a logic behavior?

Here my Interceptor:

import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpRequest, HttpHandler } from '@angular/common/http';

@Injectable({
   providedIn: 'root'
})
export class BasicAuthenticationHtppInterceptorService implements HttpInterceptor {

      constructor() { }

      intercept(req: HttpRequest<any>, next: HttpHandler) {

      if (sessionStorage.getItem('username') && sessionStorage.getItem('token')) {
          req = req.clone({
          setHeaders: {
              Authorization: JSON.stringify(sessionStorage.getItem('token'))
          }})
      }

      return next.handle(req);

    }
 }

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

>Solution :

In your main module, you should have something like:

 providers: [
    {
      provide: HTTP_INTERCEPTORS,
      useClass: BasicAuthenticationHtppInterceptorService,
      multi: true
    }
  ],
  bootstrap: [AppComponent]

See https://angular.io/guide/http-intercept-requests-and-responses for more explaination

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading