Q:

Extend HTTP - Redirect when returns unauthorized

I'm trying to redirect to the login page when I get an 401 http status.
Fist attemp was:
public getPatients(extraHttpRequestParams?: any): Observable<Array<models.Patient>> {
const path = this.basePath + '/api/patient';

let queryParameters = new URLSearchParams();
let headerParams = this.defaultHeaders;
let requestOptions: RequestOptionsArgs = {
    method: 'GET',
    headers: headerParams,
    search: queryParameters
};

return this.httpInterceptor.request(path, requestOptions)
    .map((response: Response) => {
        if (response.status === 204) {
            return undefined;
        } else {
            if (response.status === 401) {
                this.router.navigate(['/login']);
            }
            return response.json();
        }
    });

}
But when I get the 401, I does not enter to the map function, it gives an unauthorized error in the browser.
So reading some posts, there's a way to extend the http service, seems to be the correct way, but I'm getting some issues when I try to instantiate http dependencies on app.module.ts. In my case I only need to rewrite the interceptor method, but I put all the code if someone else need other part.
Here is my http extension:
import { Http, Request, RequestOptionsArgs, Response, XHRBackend, RequestOptions, ConnectionBackend, Headers} from '@angular/http';
import { Router } from '@angular/router';
import { LocationStrategy, HashLocationStrategy} from '@angular/common';
import { Observable } from 'rxjs/Observable';
import {Injectable} from '@angular/core';

@Injectable()
export class HttpInterceptor extends Http {

    constructor(backend: ConnectionBackend, defaultOptions: RequestOptions, private _router: Router) {
        super(backend, defaultOptions);
    };

    request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
        return this.intercept(super.request(url, options));
    };

    get(url: string, options?: RequestOptionsArgs): Observable<Response> {
        return this.intercept(super.get(url, options));
    };

    post(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> {
        return this.intercept(super.post(url, body, this.getRequestOptionArgs(options)));
    };

    put(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> {
        return this.intercept(super.put(url, body, this.getRequestOptionArgs(options)));
    };

    delete(url: string, options?: RequestOptionsArgs): Observable<Response> {
        return this.intercept(super.delete(url, options));
    };

    getRequestOptionArgs(options?: RequestOptionsArgs): RequestOptionsArgs {
        if (options == null) {
            options = new RequestOptions();
        }
        if (options.headers == null) {
            options.headers = new Headers();
        }
        options.headers.append('Content-Type', 'application/json');
        return options;
    };

    intercept(observable: Observable<Response>): Observable<Response> {
        return observable.catch((err, source) => {
            if (err.status == 401) {
                this._router.navigate(['/login']);
                return Observable.empty();
            } else {
                return Observable.throw(err);
            }
        });

    }
};

In my app.module.ts I have to add this:
import { NgModule } from '@angular/core';
import { BrowserModule }  from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { routing, appRoutingProviders } from './app.routing';
import { FormsModule }    from '@angular/forms';
import { Routes, RouterModule }   from '@angular/router';
import { HomeComponent } from './home/home.component';
import { PatientsComponent } from './pacientes/pacientes.component';
import { HttpInterceptor }  from '../api/api/HttpInterceptor';
import { RequestOptions, ConnectionBackend} from '@angular/http';
import { HttpModule, JsonpModule } from '@angular/http';
const routes: Routes = [

];

@NgModule({
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    JsonpModule,
    routing,
    RouterModule.forRoot(routes, { useHash: true }),  // .../#/crisis-center/
  ],
  declarations: [
    AppComponent,
    PatientsComponent,
  ],
  providers: [
    appRoutingProviders,
    HttpInterceptor,
    RequestOptions 
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

All good by now, but when I try to use the new httpInterceptor service I created, importing it and adding it to the constructor and replace http instance for my new http interceptor instance, I get a No provider for ConnectionBackend, I try to add ConnectionBackend to providers but it says 'types of property providers are incompatible'. Then I try to add httpInterceptor but I get Uncaught Error: Can't resolve all parameters for RequestOptions: (?).
So in summary there must be a way to extend http method correctly or handle the 401 in another way..
How can I do that, is there some tutorial, link or something to take a look? 

A:

There's no provider registered with the token ConnectionBackend. What you can do though is use a factory when you configure the interceptor
providers:    [
  { 
    provide: HttpInterceptor,
    useFactory: (backend: XHRBackend, options: RequestOptions, router: Router) => {
      return new HttpInterceptor(backend, options, router);
    },
    deps: [XHRBackend, RequestOptions, Router ]
  }
]

I've tested this with your code, and it should work.

