Created
February 16, 2017 08:37
-
-
Save anonymous/1a9564a38f3e1b3456302aff899ca7d4 to your computer and use it in GitHub Desktop.
http-intercept
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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