Tuesday 17 July 2018

ionic 3 with angular 5 Http Interceptor Retry request does not work

I am working on the app with use the token authentication with refresh token. I have successfully implemented the authentication and refresh token in angular v1 in a web application. In the ionic 3 app which is using angular v5 I also implemented the process. It is successfully getting the new token by refresh token. But the problem is, it is not calling the last request again.
here is my Http interceptor implementation.
@Injectable()
export class HttpsRequestInterceptor implements HttpInterceptor {
isRefreshingToken: boolean = false;
tokenSubject: BehaviorSubject<string> = new BehaviorSubject<string>(null);

constructor(
    private injector : Injector
) {

}

addToken(req: HttpRequest<any>, token: string): HttpRequest<any> {
    return req.clone({ setHeaders: { Authorization: 'Bearer ' + token }})
}

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpSentEvent | 
HttpHeaderResponse | HttpProgressEvent | HttpResponse<any> | HttpUserEvent<any>> {

    const authenticationService = this.injector.get(AuthenticationService);

    let user : AuthenticatedUser = authenticationService.getUser1();

    console.log("token", (user)? user.token : null);

    let dupReq: HttpRequest<any>;

    if (user && req.urlWithParams.indexOf("/token") < 0) {

        dupReq = this.addToken(req, user.token);

    } else {
        dupReq = req;
    }

    return next.handle(dupReq).pipe(
        tap(event => {
            if (event instanceof HttpResponse) {

            console.log(`Request for ${dupReq.urlWithParams} return status text : 
${event.statusText} .`);
            }
        }, error => {
            if (error instanceof HttpErrorResponse) {
                switch ((<HttpErrorResponse>error).status) {

                    case 401:
                        return this.handle401Error(dupReq, next);
                }
            } else{
                return Observable.throw(error);
            }
        })
    )
}

handle401Error(req: HttpRequest<any>, next: HttpHandler) {

    if (this.isRefreshingToken == false) {
        this.isRefreshingToken = true;

        this.tokenSubject.next(null);

        const authenticationService = this.injector.get(AuthenticationService);

        return authenticationService.refreshToken().then((result: ResultSet) =>{

            this.isRefreshingToken = false;

            if (result.isSuccess) {

                console.log(result.message, result.model.token);

                let user: AuthenticatedUser  = result.model;
                this.tokenSubject.next(user.token);
                return next.handle(this.addToken(req, user.token));
            } 
            // TODO logout.
        }).catch((error: ResultSet) =>{
            this.isRefreshingToken = false;

            // TODO logout.
        });
    } else {
        return this.tokenSubject
                        .filter(token => token != null)
                        .take(1)
                        .switchMap(token => {
                            return next.handle(this.addToken(req, token));
                        });
    }
}};

Anyone know why the interceptor is not repeating the last request again.
Thanks


from ionic 3 with angular 5 Http Interceptor Retry request does not work

No comments:

Post a Comment