Angular 4 Interceptor with ngx-restangular

Viewed 833

I'm using ngx-restangular with Angular 4.

I want to intercept the requests and, if the http status code is 403, I want to redirect the user to the login page.

Now I have this code on my app.module.ts

/**
 * NGX-ANGULAR configs.
 */
// Function for setting the default restangular configuration
export function RestangularConfigFactory (RestangularProvider) {

  // Set base URL
  RestangularProvider.setBaseUrl('http://192.168.1.79:8080/api/');

  // If token not valid/expired
  RestangularProvider.addErrorInterceptor((response, subject, responseHandler) => {

    if (response.status === 403) {

      // Redirect user to login page
      //Router.navigateByUrl(['login']);

      return false; // error handled
    }
    return true; // error not handled
  });
}

The Router.navigateByUrl(['login']); obviously is not working.

I've tried to import the Router from @angular/router and inject it on the AppModule constructor but then can't access the this inside RestangularConfigFactory function.

Anyone can help please?

Thank you.

1 Answers
Related