Redirect on 403


#1

I’m new on ionic. I’m trying to redirect App to registration page when my NodeJS API server rejects 403 error.

this is my states:

app.config(function($stateProvider,$urlRouterProvider){
  $stateProvider
  .state('app', {
    url: "/app",
    abstract: true,
    templateUrl: "templates/app.html"
  })
  .state('app.home', {
    url: "/home",
    views: {
      'appContent' :{
        templateUrl: "templates/home.html",
        controller : "HomeController"
      }
    }
  })

  .state('app.tabs',{
    url : '/tab',
    views : {
      'appContent' : {
        templateUrl : 'templates/tabs.html'
      }
    }
  })
  .state('app.tabs.list',{
    url : '/list',
    views : {
      'list-tab' : {
        abstract : true,
        templateUrl : 'templates/list.html',
        controller : 'ListCtrl'
      }
    }
  })
  .state('app.tabs.detail',{
    url : '/list/:rId',
    views : {
      'list-tab' : {
        templateUrl : 'templates/detail.html',
        controller : 'ListCtrl'
      }
    }
  })
  .state('register', {
    url: "/user/register",
    views: {
      'appContent' :{
        templateUrl: "templates/user/register.html",
        controller : "UsersController"
      }
    }
  });
  $urlRouterProvider.otherwise('/app/home') ;
});
app.config(['$httpProvider', function($httpProvider) {
    $httpProvider.interceptors.push('authInterceptorService');
}]);

and this is a factory i used:

app.factory('authInterceptorService', ['$q','$injector','$location',function ($q,$injector,$location){
  var responseError = function (rejection) {
    console.log(rejection);
    if (rejection.status === 403) {
      var stateService = $injector.get('$state');
      stateService.go('register',{reload: true});
    }
    return $q.reject(rejection);
  };
  return {
    responseError: responseError
  };
}]);

The /#/user/register url shown on 403 error in browser address bar, but it is still showing tabs and other stuffs of previous page! why??!!! :sob:


#2

Hello,
instead of return $q.reject(rejection); try return $q.defer(rejection);