Search code examples
angularjsngroute

AngularJs : Cancel route resolve without promise


I need to cancel my route in case of wrong login, this is my ANGULARJS NG-ROUTE code :

monApp.config(function ($routeProvider, $locationProvider){

    var rsf = { // This function tells if user is logged or not
        "check":function(stockeUtilisateur,$location,Notification){
            u = stockeUtilisateur.getUtilisateur();

                if(u.role=='admin'||u.role=='utilisateur'){
                    Notification.success("Youre logged");
                }
                else{ 
                     $location.path('/accueil');     //redirect user to home.
                    Notification.error("bad password");
                }
        }
    };

    $routeProvider
    .when('/accueil', {
      templateUrl: 'vues/accueil.html',
      controller: 'neutreCtrl'
    })
    .when('/compte', {
      templateUrl: 'vues/compte.html',
      controller: 'compteCtrl',
      resolve:rsf 
    })

    .otherwise({redirectTo: '/accueil'}); 
})

The problem is that I don't want to use $location.path() in the case of login fail, because it reloads the whole page while I simply need to stay on the route and simply cancel the route, as long as user login is wrong.

I don't know what code I could use, I've tried this instead of $location.path() : event.preventDefault(); but it doesn't work.

I've also tried resolve.cancel(); but it doesn't work too, snif !

If I remove $location.path(), then the resolve still works and i can see the "compte" view while the user is not logged.

IN other words, i don't want to get redirected, but i'd like resolve to simply do nothing in case of bad password.

Maybe you have an idea ?

Thank you .


Solution

  • The problem is that I don't want to use $location.path() in the case of login fail, because it reloads the whole page while I simply need to stay on the route and simply cancel the route, as long as user login is wrong.

    To cancel a route change from a resolver function, use a throw statement:

    monApp.config(function ($routeProvider, $locationProvider){
    
        var rsf = { // This function tells if user is logged or not
            "check":function(stockeUtilisateur,$location,Notification){
                u = stockeUtilisateur.getUtilisateur();
    
                    if(u.role=='admin'||u.role=='utilisateur'){
                        Notification.success("Youre logged");
                    }
                    else{ 
                        //$location.path('/accueil'); 
                        //Notification.error("bad password");
                        throw "bad bassword";
                    }
            }
        };
    
        $routeProvider
        .when('/accueil', {
          templateUrl: 'vues/accueil.html',
          controller: 'neutreCtrl'
        })
        .when('/compte', {
          templateUrl: 'vues/compte.html',
          controller: 'compteCtrl',
          resolve:rsf 
        })
    
        .otherwise({redirectTo: '/accueil'}); 
    })
    

    When a resolver function throws an error, the route change is prevented and a $routeChangeError event is broadcast.