Alert or navigate from HttpInterceptor

Hi all

Please see the following HttpInterceptor which checks for build version in the json returned from the server. I need to alert or navigate to another page or anything really, to let the user know if the build version of the app doesn’t match that on the back end server.

Any idea how I could get an alert to work, or navigate the user to another page etc please?

import { Injectable } from "@angular/core";
import { ConnectionBackend, RequestOptions, Request, RequestOptionsArgs, Response, Http, Headers, XHRBackend} from "@angular/http";
import { Observable } from "rxjs/Rx";


export function httpFactory(xhrBackend: XHRBackend, requestOptions: RequestOptions): Http {
    return new SMMEHttpInterceptor(xhrBackend, requestOptions);
}

@Injectable()
export class SMMEHttpInterceptor extends Http {
    constructor(
      backend: ConnectionBackend, 
      defaultOptions: RequestOptions
    ) {      
        super(backend, defaultOptions);
    }


    get(url: string, options?: RequestOptionsArgs): Observable<Response> {
        return super.get(url, options)
          .catch(this.onCatch)
          .do((res: Response) => {
            if(res.json().CURRENT_BUILD_VERSION != "1.0.3"){
              console.log("problem");
            }
          });
    }

    private onCatch(error: any, caught: Observable<any>): Observable<any> {
      return Observable.throw(error);
    }

}

It seems that a simple JavaScript alert fails on iOS. If there is a way of getting that to work then that would be fine. Just anything to display a message to the user.

Thanks!

UPDATE

Just noticed now, this answer works with current Angular and HttpClientModule not HttpModule which you still seems to use.

Therefore only consider the service, subject and subscriber part.

Interceptor with HttpClientModule:

Have a look at an answer I gave on Stackoverflow. It was never marked as correct answer but I could confirm that I use it live in production.

Summarized, use a service, a subject and subscriber the forward the event from your interceptor till your app.component where you could then navigate.

Thank you very much :slight_smile:

1 Like