I try to define in the run method of my module some simple route security.
But what ever I try, I get redirected to secure route although I'am not logged in:
// Secure routes
$rootScope.$on("$stateChangeStart", function (event, toState, toParams, fromState, fromParams) {
if ("login" === toState.name) {
return;
}
let authorized = toState.data ? toState.data.authorization : false;
if (!securityService.isAuthenticated && authorized !== true) {
$location.path("/login");
}
});
I tried $location.path("/login");, $location.url("/login");, window.location.href = "/login";, also I tried $rootScope.$apply etc. Nothing of those could make me redirect to login page.
EDIT: more detailed view
namespace testWeb {
class AppComponent implements ng.IComponentOptions {
template = `
<div class="root"><div ui-view=""></div>
</div>`;
controller = AppController;
}
interface IAppController {
}
export class AppController implements IAppController {
}
angular.module("test", [
"ui.router"
]).run(["$rootScope", "$state",
function ($rootScope, $state) {
// Secure routes
$rootScope.$on("$stateChangeStart", function (event, toState, toParams, fromState, fromParams) {
if ("login" === toState.name) {
return;
}
let authorized = toState.data ? toState.data.authorization : false;
if (!securityService.isAuthenticated && authorized !== true) {
$state.go("login");
}
});
}]).component("root", new AppComponent())
.config(($stateProvider, $urlRouterProvider, $translateProvider) => {
"ngInject";
$urlRouterProvider.otherwise("/xyz");
$stateProvider
.state("login", {
url: "/login",
templateUrl: "scripts/components/login/login.html"
});
});
}
P.S. I use Angular 1.6 with TypeScript