With Ionic 4, what is the suggested way to use a token interceptor?
I thought there was something new and built in with v4 but am not finding it back.
I am currently trying to use the following but am getting a cyclic dependency error.
import { Injectable, Injector } from '@angular/core';
import {
HttpRequest,
HttpHandler,
HttpEvent,
HttpInterceptor
} from '@angular/common/http';
import { AuthService } from './auth.service';
import { Observable } from 'rxjs';
@Injectable()
export class TokenInterceptor implements HttpInterceptor {
constructor(private injector: Injector) {}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const authService = this.injector.get(AuthService);
request = request.clone({
setHeaders: {
Authorization: 'Bearer ' +authService.getToken()
}
});
return next.handle(request);
}
}
app.module.ts:
providers: [
AuthService, {
provide: HTTP_INTERCEPTORS,
useClass: TokenInterceptor,
multi: true
}, ...
Or should I be using Angular 6 JWT Interceptor?
http://jasonwatmore.com/post/2018/05/23/angular-6-jwt-authentication-example-tutorial#jwt-interceptor-ts