23

I am trying to implement HttpInterceptor. When I add it to @NgModule, I get the following error in Chrome:

Uncaught Error: Can't resolve all parameters for JwtInterceptor: (?, ?).
    at syntaxError (compiler.js:466)
    at CompileMetadataResolver._getDependenciesMetadata (compiler.js:15547)
    at CompileMetadataResolver._getTypeMetadata (compiler.js:15382)

Spent much time on googling, don’t have an idea what to undertake…

Here is how I provide the Interceptor in AppModule:

...
providers: [
   {
      provide: HTTP_INTERCEPTORS,
      useClass: JwtInterceptor,
      multi: true
   }
],
...

Here is the Interceptor itself, nothing fancy:

export class JwtInterceptor implements HttpInterceptor {
   constructor(private inj: Injector, private logger: Logger) {
      this.logger.l(true)('Interceptor >>');
   }

   intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
      this.logger.l(true)('interceptor >>');

      const auth = this.inj.get(AuthService);

      // add token to the request:
      const authReq = req.clone({
         setHeaders: {
            Authorization: `Bearer ${auth.getToken()}`
         }
      });

      // return next.handle(authReq);
      return next.handle(authReq).do((event: HttpEvent<any>) => {
         if (event instanceof HttpResponse) {
            // todo: get refreshed token if it exists:
         }
      }, (err: any) => {
         if (err instanceof HttpErrorResponse) {
            if (err.status === 401) {
               auth.collectFailedRequest(authReq);
               // todo: redirect to the login route or show a modal
            }
         }
      });
   }
}
2
  • 12
    have you added @Injectable() to your interceptor? Dec 6, 2017 at 13:47
  • @Und3rTow , OMG, of course, your right, I haven't! Thanks so much!
    – zhekaus
    Dec 6, 2017 at 13:59

2 Answers 2

71

My bad, I forgot to add @Injectable() to my interceptor class.

2
  • 2
    I my case still not working even I have @Injectable() Aug 23, 2018 at 11:14
  • i added @Injectable() but still get this error : Can't resolve all parameters for HttpErrorInterceptor
    – sajjad
    Oct 30, 2020 at 14:11
1

add @Injectable() in your interceptor file after import statements

import { Injectable } from "@angular/core";
import { Observable, throwError } from "rxjs";
import { tap } from 'rxjs/operators';
import { Router } from '@angular/router';
import { ToastController } from '@ionic/angular';

@Injectable() <- add this in your interceptor file ...(maybe you have missed by mistake)

export class JwtInterceptor implements HttpInterceptor {
  constructor(private router: Router,public toastController: ToastController) {

  }
  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

    return next.handle(request).pipe(
      tap(
        event =>  {
          if (event instanceof HttpResponse) {
            // do stuff with response if you want
          }
        },

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.