0

So I have some code like this:

    if(result.data.authenticated === false) {
        //need to send to unauthoirzed page. 
        $location.path('/unauthorized');
    }

and when it hits I see my url change to:

/#/unauthorized

but it's not displaying my HTML page that I have defined here:

 .when('/unauthorized', {
                templateUrl:'partials/unauthorized.html',
                controller:'unauthorizedController'
            })

just stays on the original page.

David Dennis
  • 702
  • 2
  • 9
  • 26

2 Answers2

0

This is a full example of AngularJs routing with authorizing. Hope this helps you

(function() {
  'use strict';
  var app = angular.module('app', [
    'ngRoute',
  ]);

  window.routes = {
    '/': {
      templateUrl: 'app/visitor/visitor.html',
      anonymous: true
    },
    '/index': {
      templateUrl: 'app/visitor/visitor.html',
      caseInsensitiveMatch: true,
      anonymous: true
    },
    '/lu/:mt?/:tab?/:id?': {
      templateUrl: 'app/user/user.html',
      caseInsensitiveMatch: true,
      anonymous: false
    }
  };

  app.config(function($routeProvider, $locationProvider) {
    for (var path in window.routes) {
      $routeProvider.when(path, window.routes[path]);
    }
    $routeProvider.otherwise({
      redirectTo: '/'
    });

    $locationProvider.html5Mode(true);
  });

  app.config(function($routeProvider, $locationProvider) {
    for (var path in window.routes) {
      $routeProvider.when(path, window.routes[path]);
    }
    $routeProvider.otherwise({
      redirectTo: '/'
    });

    $locationProvider.html5Mode(true);
  });

  app.run(function($rootScope, toastr, sessionService, $location) {
    $rootScope.$on('$routeChangeStart', function(event, next, current) {
      for (var i in window.routes) {
        if (next && next.hasOwnProperty('$$route') && next.$$route.hasOwnProperty('originalPath') &&
          next.$$route.originalPath.localeCompare(i) == 0) {
          if (!window.routes[i].anonymous && !sessionService.getUserAuthenticated()) {
            toastr.error('Access not found');
            event.preventDefault();
            $location.path('/');
          } else if (window.routes[i].anonymous && sessionService.getUserAuthenticated()) {
            event.preventDefault();
            $location.path('/aa/xx/ff');
          }
        }
      }
    });
  });

})();
Saeed
  • 5,413
  • 3
  • 26
  • 40
-1

to test if the route updated successfully, in unauthorizedController controller add log or debugger to test if the router changed or not, and replate templateUrl with template and see the result

Mhd Wael Jazmati
  • 636
  • 9
  • 18