Skip to content

Instantly share code, notes, and snippets.

Created February 16, 2017 08:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/1a9564a38f3e1b3456302aff899ca7d4 to your computer and use it in GitHub Desktop.
Save anonymous/1a9564a38f3e1b3456302aff899ca7d4 to your computer and use it in GitHub Desktop.
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