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
1 Answer
Reset to default 4You 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
- Add
HttpClientModule
in imports array inapp.module.ts
- Import
HttpClient
in yourauthentication.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