While developing an angular interceptor to verify the validity of my auth tokens, I encountered an issue where the do
method is not recognized by angular. The alternative solution involving subscribe
was effective, but it led to duplicate requests being sent to the server.
TypeError: next.handle(...).do is not a function
at AuthTokenService.webpackJsonp.../../../../../src/app/commons/services/interceptors/auth-token.service.ts.AuthTokenService.intercept (auth-token.service.ts:37)
at HttpInterceptorHandler.webpackJsonp.../../../common/esm5/http.js.HttpInterceptorHandler.handle (http.js:1796)
at XsrfService.webpackJsonp.../../../../../src/app/commons/services/interceptors/xsrf.service.ts.XsrfService.intercept (xsrf.service.ts:15)
at HttpInterceptorHandler.webpackJsonp.../../../common/esm5/http.js.HttpInterceptorHandler.handle (http.js:1796)
at HttpXsrfInterceptor.webpackJsonp.../../../common/esm5/http.js.HttpXsrfInterceptor.intercept (http.js:2489)
at HttpInterceptorHandler.webpackJsonp.../../../common/esm5/http.js.HttpInterceptorHandler.handle (http.js:1796)
at MergeMapSubscriber.project (http.js:1466)
at MergeMapSubscriber.webpackJsonp.../../../../rxjs/_esm5/operators/mergeMap.js.MergeMapSubscriber._tryNext (mergeMap.js:128)
at MergeMapSubscriber.webpackJsonp.../../../../rxjs/_esm5/operators/mergeMap.js.MergeMapSubscriber._next (mergeMap.js:118)
at MergeMapSubscriber.webpackJsonp.../../../../rxjs/_esm5/Subscriber.js.Subscriber.next (Subscriber.js:92)
Take a look at my interceptor code below:
import { Injectable, NgModule} from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest} from
'@angular/common/http';
import { SessionService } from 'app/commons/services/auth/session.service';
import { HttpErrorResponse } from "@angular/common/http";
import { StateService } from '@uirouter/angular';
import 'rxjs/add/operator/do';
import * as _ from "lodash";
@Injectable()
export class AuthTokenService implements HttpInterceptor {
constructor(
private sessionService: SessionService,
private stateService: StateService
) {}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const currUser = this.sessionService.getCurrentUser();
const authToken = _.get(currUser, ['auth_token'], null);
let dupReq = req.clone({ headers: req.headers.set('Authorization', '') });
if (!_.isNil(authToken)) {
dupReq = req.clone({ headers: req.headers.set('Authorization', `Token ${authToken}`) });
}
return next.handle(dupReq)
.do(ev => {
console.log(event);
})
}
};
Although I have followed all the necessary steps, the do
side-effect mentioned in the guide does not seem to be working as expected.