Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
http-intercept
// Intercept Service
import { NavController, NavParams } from 'ionic-angular';
import { Request, XHRBackend, BrowserXhr, ResponseOptions, XSRFStrategy, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
import { HomePage } from '../pages/home/home';
export class AuthenticatedHttpService extends XHRBackend {
constructor(
_browserXhr: BrowserXhr,
_baseResponseOptions: ResponseOptions,
_xsrfStrategy: XSRFStrategy,
public navCtrl: NavController
) {
super(_browserXhr, _baseResponseOptions, _xsrfStrategy);
}
createConnection(request: Request) {
let xhrConnection = super.createConnection(request);
xhrConnection.response = xhrConnection.response.catch((error: Response) => {
console.log('HERE IN INTERCEPT', error, this.navCtrl);
if ((error.status === 401 || error.status === 403) && (window.location.href.match(/\?/g) || []).length < 2) {
// ********* This is where iam redirecting **************//
this.navCtrl.setRoot(HomePage);
}
return Observable.throw(error);
});
return xhrConnection;
}
}
// ************* Added in the modules ***********//
//**********************************************//
providers: [
{ provide: XHRBackend, useClass: AuthenticatedHttpService },
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.