javascript - Angular 5 http interceptor not intercepting - Stack Overflow

I've got a correct JWT token stored in local storage and an interceptor that I blatantly copied fr

I've got a correct JWT token stored in local storage and an interceptor that I blatantly copied from a tutorial. However, it doesn't intercept and add headers to requests.

Here's where I make a request:

.service.ts

here's the interceptor:

.interceptor.ts

and my appmodule - I'm pretty sure it's correctly imported:

.module.ts

When I make a request I expect the interceptor to log messages that I specified to console and add the token to header, it doesn't do that though and I've got no idea why :/ I checked the code with some other tutorials found online and didn't see any differences capable of breaking my code. I also don't have enough experience with Angular to debug it properly.

Any help would be much apprecieated.

I've got a correct JWT token stored in local storage and an interceptor that I blatantly copied from a tutorial. However, it doesn't intercept and add headers to requests.

Here's where I make a request:

https://github./Marred/Informakcja/blob/master/Informakcja-client/src/app/services/information.service.ts

here's the interceptor:

https://github./Marred/Informakcja/blob/master/Informakcja-client/src/app/services/token.interceptor.ts

and my appmodule - I'm pretty sure it's correctly imported:

https://github./Marred/Informakcja/blob/master/Informakcja-client/src/app/app.module.ts

When I make a request I expect the interceptor to log messages that I specified to console and add the token to header, it doesn't do that though and I've got no idea why :/ I checked the code with some other tutorials found online and didn't see any differences capable of breaking my code. I also don't have enough experience with Angular to debug it properly.

Any help would be much apprecieated.

Share Improve this question edited Jan 22, 2021 at 10:53 user1608841 2,4751 gold badge29 silver badges44 bronze badges asked Mar 5, 2018 at 9:31 NechNech 3791 gold badge3 silver badges15 bronze badges 1
  • Links are dead. – Jacopo Lanzoni Commented Aug 20, 2018 at 8:39
Add a ment  | 

1 Answer 1

Reset to default 4

You are doing couple of things wrongly here:

Interceptors are used with HttpClientModule not with HttpModule. You are using HttpModule. You need to change to HttpClientModule

  1. Add HttpClientModule in imports array in app.module.ts
  2. Import HttpClient in your authentication.service.ts and take its reference in its constructor.

Refer below code:

  //app.module.ts
    import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/mon/http';
    .
    .
    .
    @NgModule({
      declarations: [
        AppComponent,
       .
       .
       .
      ],
      imports: [
        BrowserModule,
        .
        .
        .,
        HttpClientModule, //add here
        RouterModule.forRoot([
          { path: '', redirectTo: 'home', pathMatch: 'full' },
          { path: 'home', ponent: HomeComponent },
          { path: 'login', ponent: LoginComponent },
          { path: 'register', ponent: RegisterComponent },
          { path: 'add-information', ponent: AddInformationComponent },
          { path: '**', redirectTo: 'home' }
        ], { useHash: true })
      ],
      providers: [
        { provide: 'BASE_URL', useValue: 'https://some URL/' },
        UserService,
        InformationService,
        AuthenticationService,
        AlertService,
        {
          provide: HTTP_INTERCEPTORS,
          useClass: TokenInterceptor,
          multi: true
        }
      ],
      bootstrap: [AppComponent]
    })
    export class AppModule { }

and

//information.service.ts and authentication.service.ts

import { Injectable, Inject } from '@angular/core';
import { HttpClient} from '@angular/mon/http'; //added import
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';

@Injectable()
export class InformationService {

  constructor(private http: HttpClient, @Inject('BASE_URL') private baseUrl: string) { } //made changes in cunstructor

  add(link: string, title: string, description: string) {
    return this.http.post(this.baseUrl + 'api/Information', { link: link, title: title, description: description })
    .map((response: Response) => {
        console.log(response);
    });
  }
}

make similar changes in authentication.service.ts

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745310708a4621985.html

相关推荐

  • javascript - Angular 5 http interceptor not intercepting - Stack Overflow

    I've got a correct JWT token stored in local storage and an interceptor that I blatantly copied fr

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信