Extend HTTP - Redirect when returns unauthorized
<p>I'm trying to redirect to the login page when I get an 401 http status.
Fist attemp was:</p>

<pre><code>public getPatients(extraHttpRequestParams?: any): Observable&lt;Array&lt;models.Patient&gt;&gt; {
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) =&gt; {
        if (response.status === 204) {
            return undefined;
        } else {
            if (response.status === 401) {
                this.router.navigate(['/login']);
            }
            return response.json();
        }
    });
</code></pre>

<p>}</p>

<p>But when I get the 401, I does not enter to the map function, it gives an unauthorized error in the browser.</p>

<p>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.</p>

<p>Here is my http extension:</p>

<pre><code>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&lt;Response&gt; {
        return this.intercept(super.request(url, options));
    };

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

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

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

    delete(url: string, options?: RequestOptionsArgs): Observable&lt;Response&gt; {
        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&lt;Response&gt;): Observable&lt;Response&gt; {
        return observable.catch((err, source) =&gt; {
            if (err.status == 401) {
                this._router.navigate(['/login']);
                return Observable.empty();
            } else {
                return Observable.throw(err);
            }
        });

    }
};
</code></pre>

<p>In my app.module.ts I have to add this:</p>

<pre><code>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 { }
</code></pre>

<p>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: (?).</p>

<p>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? </p>